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.

1235 lines
43KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2016 - ROLI Ltd.
  5. Permission is granted to use this software under the terms of the ISC license
  6. http://www.isc.org/downloads/software-support-policy/isc-license/
  7. Permission to use, copy, modify, and/or distribute this software for any
  8. purpose with or without fee is hereby granted, provided that the above
  9. copyright notice and this permission notice appear in all copies.
  10. THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH REGARD
  11. TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
  12. FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT,
  13. OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
  14. USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  15. TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  16. OF THIS SOFTWARE.
  17. -----------------------------------------------------------------------------
  18. To release a closed-source product which uses other parts of JUCE not
  19. licensed under the ISC terms, commercial licenses are available: visit
  20. www.juce.com for more information.
  21. ==============================================================================
  22. */
  23. package com.juce;
  24. import android.app.Activity;
  25. import android.app.AlertDialog;
  26. import android.content.DialogInterface;
  27. import android.content.Context;
  28. import android.content.Intent;
  29. import android.content.res.Configuration;
  30. import android.content.pm.PackageInfo;
  31. import android.content.pm.PackageManager;
  32. import android.net.Uri;
  33. import android.os.Bundle;
  34. import android.os.Looper;
  35. import android.os.Handler;
  36. import android.os.ParcelUuid;
  37. import android.os.Environment;
  38. import android.view.*;
  39. import android.view.inputmethod.BaseInputConnection;
  40. import android.view.inputmethod.EditorInfo;
  41. import android.view.inputmethod.InputConnection;
  42. import android.view.inputmethod.InputMethodManager;
  43. import android.graphics.*;
  44. import android.text.ClipboardManager;
  45. import android.text.InputType;
  46. import android.util.DisplayMetrics;
  47. import android.util.Log;
  48. import java.lang.Runnable;
  49. import java.util.*;
  50. import java.io.*;
  51. import java.net.URL;
  52. import java.net.HttpURLConnection;
  53. import android.media.AudioManager;
  54. import android.media.MediaScannerConnection;
  55. import android.media.MediaScannerConnection.MediaScannerConnectionClient;
  56. import android.support.v4.content.ContextCompat;
  57. import android.support.v4.app.ActivityCompat;
  58. import android.Manifest;
  59. $$JuceAndroidMidiImports$$ // If you get an error here, you need to re-save your project with the Projucer!
  60. //==============================================================================
  61. public class JuceAppActivity extends Activity
  62. {
  63. //==============================================================================
  64. static
  65. {
  66. System.loadLibrary ("juce_jni");
  67. }
  68. //==============================================================================
  69. public boolean isPermissionDeclaredInManifest (int permissionID)
  70. {
  71. String permissionToCheck = getAndroidPermissionName(permissionID);
  72. try
  73. {
  74. PackageInfo info = getPackageManager().getPackageInfo(getApplicationContext().getPackageName(), PackageManager.GET_PERMISSIONS);
  75. if (info.requestedPermissions != null)
  76. for (String permission : info.requestedPermissions)
  77. if (permission.equals (permissionToCheck))
  78. return true;
  79. }
  80. catch (PackageManager.NameNotFoundException e)
  81. {
  82. Log.d ("JUCE", "isPermissionDeclaredInManifest: PackageManager.NameNotFoundException = " + e.toString());
  83. }
  84. Log.d ("JUCE", "isPermissionDeclaredInManifest: could not find requested permission " + permissionToCheck);
  85. return false;
  86. }
  87. //==============================================================================
  88. // these have to match the values of enum PermissionID in C++ class RuntimePermissions:
  89. private static final int JUCE_PERMISSIONS_RECORD_AUDIO = 1;
  90. private static final int JUCE_PERMISSIONS_BLUETOOTH_MIDI = 2;
  91. private static String getAndroidPermissionName (int permissionID)
  92. {
  93. switch (permissionID)
  94. {
  95. case JUCE_PERMISSIONS_RECORD_AUDIO: return Manifest.permission.RECORD_AUDIO;
  96. case JUCE_PERMISSIONS_BLUETOOTH_MIDI: return Manifest.permission.ACCESS_COARSE_LOCATION;
  97. }
  98. // unknown permission ID!
  99. assert false;
  100. return new String();
  101. }
  102. public boolean isPermissionGranted (int permissionID)
  103. {
  104. return ContextCompat.checkSelfPermission (this, getAndroidPermissionName (permissionID)) == PackageManager.PERMISSION_GRANTED;
  105. }
  106. private Map<Integer, Long> permissionCallbackPtrMap;
  107. public void requestRuntimePermission (int permissionID, long ptrToCallback)
  108. {
  109. String permissionName = getAndroidPermissionName (permissionID);
  110. if (ContextCompat.checkSelfPermission (this, permissionName) != PackageManager.PERMISSION_GRANTED)
  111. {
  112. // remember callbackPtr, request permissions, and let onRequestPermissionResult call callback asynchronously
  113. permissionCallbackPtrMap.put (permissionID, ptrToCallback);
  114. ActivityCompat.requestPermissions (this, new String[]{permissionName}, permissionID);
  115. }
  116. else
  117. {
  118. // permissions were already granted before, we can call callback directly
  119. androidRuntimePermissionsCallback (true, ptrToCallback);
  120. }
  121. }
  122. private native void androidRuntimePermissionsCallback (boolean permissionWasGranted, long ptrToCallback);
  123. $$JuceAndroidRuntimePermissionsCode$$ // If you get an error here, you need to re-save your project with the Projucer!
  124. //==============================================================================
  125. public static class MidiPortID extends Object
  126. {
  127. public MidiPortID (int index, boolean direction)
  128. {
  129. androidIndex = index;
  130. isInput = direction;
  131. }
  132. public int androidIndex;
  133. public boolean isInput;
  134. @Override
  135. public int hashCode()
  136. {
  137. Integer i = new Integer (androidIndex);
  138. return i.hashCode() * (isInput ? -1 : 1);
  139. }
  140. @Override
  141. public boolean equals (Object obj)
  142. {
  143. if (obj == null)
  144. return false;
  145. if (getClass() != obj.getClass())
  146. return false;
  147. MidiPortID other = (MidiPortID) obj;
  148. return (androidIndex == other.androidIndex && isInput == other.isInput);
  149. }
  150. }
  151. public interface JuceMidiPort
  152. {
  153. boolean isInputPort();
  154. // start, stop does nothing on an output port
  155. void start();
  156. void stop();
  157. void close();
  158. MidiPortID getPortId();
  159. // send will do nothing on an input port
  160. void sendMidi (byte[] msg, int offset, int count);
  161. }
  162. //==============================================================================
  163. $$JuceAndroidMidiCode$$ // If you get an error here, you need to re-save your project with the Projucer!
  164. //==============================================================================
  165. @Override
  166. public void onCreate (Bundle savedInstanceState)
  167. {
  168. super.onCreate (savedInstanceState);
  169. isScreenSaverEnabled = true;
  170. hideActionBar();
  171. viewHolder = new ViewHolder (this);
  172. setContentView (viewHolder);
  173. setVolumeControlStream (AudioManager.STREAM_MUSIC);
  174. permissionCallbackPtrMap = new HashMap<Integer, Long>();
  175. }
  176. @Override
  177. protected void onDestroy()
  178. {
  179. quitApp();
  180. super.onDestroy();
  181. clearDataCache();
  182. }
  183. @Override
  184. protected void onPause()
  185. {
  186. suspendApp();
  187. try
  188. {
  189. Thread.sleep (1000); // This is a bit of a hack to avoid some hard-to-track-down
  190. // openGL glitches when pausing/resuming apps..
  191. } catch (InterruptedException e) {}
  192. super.onPause();
  193. }
  194. @Override
  195. protected void onResume()
  196. {
  197. super.onResume();
  198. resumeApp();
  199. }
  200. @Override
  201. public void onConfigurationChanged (Configuration cfg)
  202. {
  203. super.onConfigurationChanged (cfg);
  204. setContentView (viewHolder);
  205. }
  206. private void callAppLauncher()
  207. {
  208. launchApp (getApplicationInfo().publicSourceDir,
  209. getApplicationInfo().dataDir);
  210. }
  211. private void hideActionBar()
  212. {
  213. // get "getActionBar" method
  214. java.lang.reflect.Method getActionBarMethod = null;
  215. try
  216. {
  217. getActionBarMethod = this.getClass().getMethod ("getActionBar");
  218. }
  219. catch (SecurityException e) { return; }
  220. catch (NoSuchMethodException e) { return; }
  221. if (getActionBarMethod == null) return;
  222. // invoke "getActionBar" method
  223. Object actionBar = null;
  224. try
  225. {
  226. actionBar = getActionBarMethod.invoke (this);
  227. }
  228. catch (java.lang.IllegalArgumentException e) { return; }
  229. catch (java.lang.IllegalAccessException e) { return; }
  230. catch (java.lang.reflect.InvocationTargetException e) { return; }
  231. if (actionBar == null) return;
  232. // get "hide" method
  233. java.lang.reflect.Method actionBarHideMethod = null;
  234. try
  235. {
  236. actionBarHideMethod = actionBar.getClass().getMethod ("hide");
  237. }
  238. catch (SecurityException e) { return; }
  239. catch (NoSuchMethodException e) { return; }
  240. if (actionBarHideMethod == null) return;
  241. // invoke "hide" method
  242. try
  243. {
  244. actionBarHideMethod.invoke (actionBar);
  245. }
  246. catch (java.lang.IllegalArgumentException e) {}
  247. catch (java.lang.IllegalAccessException e) {}
  248. catch (java.lang.reflect.InvocationTargetException e) {}
  249. }
  250. //==============================================================================
  251. private native void launchApp (String appFile, String appDataDir);
  252. private native void quitApp();
  253. private native void suspendApp();
  254. private native void resumeApp();
  255. private native void setScreenSize (int screenWidth, int screenHeight, int dpi);
  256. //==============================================================================
  257. public native void deliverMessage (long value);
  258. private android.os.Handler messageHandler = new android.os.Handler();
  259. public final void postMessage (long value)
  260. {
  261. messageHandler.post (new MessageCallback (value));
  262. }
  263. private final class MessageCallback implements Runnable
  264. {
  265. public MessageCallback (long value_) { value = value_; }
  266. public final void run() { deliverMessage (value); }
  267. private long value;
  268. }
  269. //==============================================================================
  270. private ViewHolder viewHolder;
  271. private MidiDeviceManager midiDeviceManager = null;
  272. private BluetoothManager bluetoothManager = null;
  273. private boolean isScreenSaverEnabled;
  274. private java.util.Timer keepAliveTimer;
  275. public final ComponentPeerView createNewView (boolean opaque, long host)
  276. {
  277. ComponentPeerView v = new ComponentPeerView (this, opaque, host);
  278. viewHolder.addView (v);
  279. return v;
  280. }
  281. public final void deleteView (ComponentPeerView view)
  282. {
  283. ViewGroup group = (ViewGroup) (view.getParent());
  284. if (group != null)
  285. group.removeView (view);
  286. }
  287. public final void deleteNativeSurfaceView (NativeSurfaceView view)
  288. {
  289. ViewGroup group = (ViewGroup) (view.getParent());
  290. if (group != null)
  291. group.removeView (view);
  292. }
  293. final class ViewHolder extends ViewGroup
  294. {
  295. public ViewHolder (Context context)
  296. {
  297. super (context);
  298. setDescendantFocusability (ViewGroup.FOCUS_AFTER_DESCENDANTS);
  299. setFocusable (false);
  300. }
  301. protected final void onLayout (boolean changed, int left, int top, int right, int bottom)
  302. {
  303. setScreenSize (getWidth(), getHeight(), getDPI());
  304. if (isFirstResize)
  305. {
  306. isFirstResize = false;
  307. callAppLauncher();
  308. }
  309. }
  310. private final int getDPI()
  311. {
  312. DisplayMetrics metrics = new DisplayMetrics();
  313. getWindowManager().getDefaultDisplay().getMetrics (metrics);
  314. return metrics.densityDpi;
  315. }
  316. private boolean isFirstResize = true;
  317. }
  318. public final void excludeClipRegion (android.graphics.Canvas canvas, float left, float top, float right, float bottom)
  319. {
  320. canvas.clipRect (left, top, right, bottom, android.graphics.Region.Op.DIFFERENCE);
  321. }
  322. //==============================================================================
  323. public final void setScreenSaver (boolean enabled)
  324. {
  325. if (isScreenSaverEnabled != enabled)
  326. {
  327. isScreenSaverEnabled = enabled;
  328. if (keepAliveTimer != null)
  329. {
  330. keepAliveTimer.cancel();
  331. keepAliveTimer = null;
  332. }
  333. if (enabled)
  334. {
  335. getWindow().clearFlags (WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
  336. }
  337. else
  338. {
  339. getWindow().addFlags (WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
  340. // If no user input is received after about 3 seconds, the OS will lower the
  341. // task's priority, so this timer forces it to be kept active.
  342. keepAliveTimer = new java.util.Timer();
  343. keepAliveTimer.scheduleAtFixedRate (new TimerTask()
  344. {
  345. @Override
  346. public void run()
  347. {
  348. android.app.Instrumentation instrumentation = new android.app.Instrumentation();
  349. try
  350. {
  351. instrumentation.sendKeyDownUpSync (KeyEvent.KEYCODE_UNKNOWN);
  352. }
  353. catch (Exception e)
  354. {
  355. }
  356. }
  357. }, 2000, 2000);
  358. }
  359. }
  360. }
  361. public final boolean getScreenSaver()
  362. {
  363. return isScreenSaverEnabled;
  364. }
  365. //==============================================================================
  366. public final String getClipboardContent()
  367. {
  368. ClipboardManager clipboard = (ClipboardManager) getSystemService (CLIPBOARD_SERVICE);
  369. return clipboard.getText().toString();
  370. }
  371. public final void setClipboardContent (String newText)
  372. {
  373. ClipboardManager clipboard = (ClipboardManager) getSystemService (CLIPBOARD_SERVICE);
  374. clipboard.setText (newText);
  375. }
  376. //==============================================================================
  377. public final void showMessageBox (String title, String message, final long callback)
  378. {
  379. AlertDialog.Builder builder = new AlertDialog.Builder (this);
  380. builder.setTitle (title)
  381. .setMessage (message)
  382. .setCancelable (true)
  383. .setPositiveButton ("OK", new DialogInterface.OnClickListener()
  384. {
  385. public void onClick (DialogInterface dialog, int id)
  386. {
  387. dialog.cancel();
  388. JuceAppActivity.this.alertDismissed (callback, 0);
  389. }
  390. });
  391. builder.create().show();
  392. }
  393. public final void showOkCancelBox (String title, String message, final long callback)
  394. {
  395. AlertDialog.Builder builder = new AlertDialog.Builder (this);
  396. builder.setTitle (title)
  397. .setMessage (message)
  398. .setCancelable (true)
  399. .setPositiveButton ("OK", new DialogInterface.OnClickListener()
  400. {
  401. public void onClick (DialogInterface dialog, int id)
  402. {
  403. dialog.cancel();
  404. JuceAppActivity.this.alertDismissed (callback, 1);
  405. }
  406. })
  407. .setNegativeButton ("Cancel", new DialogInterface.OnClickListener()
  408. {
  409. public void onClick (DialogInterface dialog, int id)
  410. {
  411. dialog.cancel();
  412. JuceAppActivity.this.alertDismissed (callback, 0);
  413. }
  414. });
  415. builder.create().show();
  416. }
  417. public final void showYesNoCancelBox (String title, String message, final long callback)
  418. {
  419. AlertDialog.Builder builder = new AlertDialog.Builder (this);
  420. builder.setTitle (title)
  421. .setMessage (message)
  422. .setCancelable (true)
  423. .setPositiveButton ("Yes", new DialogInterface.OnClickListener()
  424. {
  425. public void onClick (DialogInterface dialog, int id)
  426. {
  427. dialog.cancel();
  428. JuceAppActivity.this.alertDismissed (callback, 1);
  429. }
  430. })
  431. .setNegativeButton ("No", new DialogInterface.OnClickListener()
  432. {
  433. public void onClick (DialogInterface dialog, int id)
  434. {
  435. dialog.cancel();
  436. JuceAppActivity.this.alertDismissed (callback, 2);
  437. }
  438. })
  439. .setNeutralButton ("Cancel", new DialogInterface.OnClickListener()
  440. {
  441. public void onClick (DialogInterface dialog, int id)
  442. {
  443. dialog.cancel();
  444. JuceAppActivity.this.alertDismissed (callback, 0);
  445. }
  446. });
  447. builder.create().show();
  448. }
  449. public native void alertDismissed (long callback, int id);
  450. //==============================================================================
  451. public final class ComponentPeerView extends ViewGroup
  452. implements View.OnFocusChangeListener
  453. {
  454. public ComponentPeerView (Context context, boolean opaque_, long host)
  455. {
  456. super (context);
  457. this.host = host;
  458. setWillNotDraw (false);
  459. opaque = opaque_;
  460. setFocusable (true);
  461. setFocusableInTouchMode (true);
  462. setOnFocusChangeListener (this);
  463. requestFocus();
  464. // swap red and blue colours to match internal opengl texture format
  465. ColorMatrix colorMatrix = new ColorMatrix();
  466. float[] colorTransform = { 0, 0, 1.0f, 0, 0,
  467. 0, 1.0f, 0, 0, 0,
  468. 1.0f, 0, 0, 0, 0,
  469. 0, 0, 0, 1.0f, 0 };
  470. colorMatrix.set (colorTransform);
  471. paint.setColorFilter (new ColorMatrixColorFilter (colorMatrix));
  472. }
  473. //==============================================================================
  474. private native void handlePaint (long host, Canvas canvas, Paint paint);
  475. @Override
  476. public void onDraw (Canvas canvas)
  477. {
  478. handlePaint (host, canvas, paint);
  479. }
  480. @Override
  481. public boolean isOpaque()
  482. {
  483. return opaque;
  484. }
  485. private boolean opaque;
  486. private long host;
  487. private Paint paint = new Paint();
  488. //==============================================================================
  489. private native void handleMouseDown (long host, int index, float x, float y, long time);
  490. private native void handleMouseDrag (long host, int index, float x, float y, long time);
  491. private native void handleMouseUp (long host, int index, float x, float y, long time);
  492. @Override
  493. public boolean onTouchEvent (MotionEvent event)
  494. {
  495. int action = event.getAction();
  496. long time = event.getEventTime();
  497. switch (action & MotionEvent.ACTION_MASK)
  498. {
  499. case MotionEvent.ACTION_DOWN:
  500. handleMouseDown (host, event.getPointerId(0), event.getX(), event.getY(), time);
  501. return true;
  502. case MotionEvent.ACTION_CANCEL:
  503. case MotionEvent.ACTION_UP:
  504. handleMouseUp (host, event.getPointerId(0), event.getX(), event.getY(), time);
  505. return true;
  506. case MotionEvent.ACTION_MOVE:
  507. {
  508. int n = event.getPointerCount();
  509. for (int i = 0; i < n; ++i)
  510. handleMouseDrag (host, event.getPointerId(i), event.getX(i), event.getY(i), time);
  511. return true;
  512. }
  513. case MotionEvent.ACTION_POINTER_UP:
  514. {
  515. int i = (action & MotionEvent.ACTION_POINTER_INDEX_MASK) >> MotionEvent.ACTION_POINTER_INDEX_SHIFT;
  516. handleMouseUp (host, event.getPointerId(i), event.getX(i), event.getY(i), time);
  517. return true;
  518. }
  519. case MotionEvent.ACTION_POINTER_DOWN:
  520. {
  521. int i = (action & MotionEvent.ACTION_POINTER_INDEX_MASK) >> MotionEvent.ACTION_POINTER_INDEX_SHIFT;
  522. handleMouseDown (host, event.getPointerId(i), event.getX(i), event.getY(i), time);
  523. return true;
  524. }
  525. default:
  526. break;
  527. }
  528. return false;
  529. }
  530. //==============================================================================
  531. private native void handleKeyDown (long host, int keycode, int textchar);
  532. private native void handleKeyUp (long host, int keycode, int textchar);
  533. public void showKeyboard (String type)
  534. {
  535. InputMethodManager imm = (InputMethodManager) getSystemService (Context.INPUT_METHOD_SERVICE);
  536. if (imm != null)
  537. {
  538. if (type.length() > 0)
  539. {
  540. imm.showSoftInput (this, android.view.inputmethod.InputMethodManager.SHOW_IMPLICIT);
  541. imm.setInputMethod (getWindowToken(), type);
  542. }
  543. else
  544. {
  545. imm.hideSoftInputFromWindow (getWindowToken(), 0);
  546. }
  547. }
  548. }
  549. @Override
  550. public boolean onKeyDown (int keyCode, KeyEvent event)
  551. {
  552. switch (keyCode)
  553. {
  554. case KeyEvent.KEYCODE_VOLUME_UP:
  555. case KeyEvent.KEYCODE_VOLUME_DOWN:
  556. return super.onKeyDown (keyCode, event);
  557. default: break;
  558. }
  559. handleKeyDown (host, keyCode, event.getUnicodeChar());
  560. return true;
  561. }
  562. @Override
  563. public boolean onKeyUp (int keyCode, KeyEvent event)
  564. {
  565. handleKeyUp (host, keyCode, event.getUnicodeChar());
  566. return true;
  567. }
  568. @Override
  569. public boolean onKeyMultiple (int keyCode, int count, KeyEvent event)
  570. {
  571. if (keyCode != KeyEvent.KEYCODE_UNKNOWN || event.getAction() != KeyEvent.ACTION_MULTIPLE)
  572. return super.onKeyMultiple (keyCode, count, event);
  573. if (event.getCharacters() != null)
  574. {
  575. int utf8Char = event.getCharacters().codePointAt (0);
  576. handleKeyDown (host, utf8Char, utf8Char);
  577. return true;
  578. }
  579. return false;
  580. }
  581. // this is here to make keyboard entry work on a Galaxy Tab2 10.1
  582. @Override
  583. public InputConnection onCreateInputConnection (EditorInfo outAttrs)
  584. {
  585. outAttrs.actionLabel = "";
  586. outAttrs.hintText = "";
  587. outAttrs.initialCapsMode = 0;
  588. outAttrs.initialSelEnd = outAttrs.initialSelStart = -1;
  589. outAttrs.label = "";
  590. outAttrs.imeOptions = EditorInfo.IME_ACTION_DONE | EditorInfo.IME_FLAG_NO_EXTRACT_UI;
  591. outAttrs.inputType = InputType.TYPE_NULL;
  592. return new BaseInputConnection (this, false);
  593. }
  594. //==============================================================================
  595. @Override
  596. protected void onSizeChanged (int w, int h, int oldw, int oldh)
  597. {
  598. super.onSizeChanged (w, h, oldw, oldh);
  599. viewSizeChanged (host);
  600. }
  601. @Override
  602. protected void onLayout (boolean changed, int left, int top, int right, int bottom)
  603. {
  604. for (int i = getChildCount(); --i >= 0;)
  605. requestTransparentRegion (getChildAt (i));
  606. }
  607. private native void viewSizeChanged (long host);
  608. @Override
  609. public void onFocusChange (View v, boolean hasFocus)
  610. {
  611. if (v == this)
  612. focusChanged (host, hasFocus);
  613. }
  614. private native void focusChanged (long host, boolean hasFocus);
  615. public void setViewName (String newName) {}
  616. public boolean isVisible() { return getVisibility() == VISIBLE; }
  617. public void setVisible (boolean b) { setVisibility (b ? VISIBLE : INVISIBLE); }
  618. public boolean containsPoint (int x, int y)
  619. {
  620. return true; //xxx needs to check overlapping views
  621. }
  622. }
  623. //==============================================================================
  624. public static class NativeSurfaceView extends SurfaceView
  625. implements SurfaceHolder.Callback
  626. {
  627. private long nativeContext = 0;
  628. NativeSurfaceView (Context context, long nativeContextPtr)
  629. {
  630. super (context);
  631. nativeContext = nativeContextPtr;
  632. }
  633. public Surface getNativeSurface()
  634. {
  635. Surface retval = null;
  636. SurfaceHolder holder = getHolder();
  637. if (holder != null)
  638. retval = holder.getSurface();
  639. return retval;
  640. }
  641. //==============================================================================
  642. @Override
  643. public void surfaceChanged (SurfaceHolder holder, int format, int width, int height)
  644. {
  645. surfaceChangedNative (nativeContext, holder, format, width, height);
  646. }
  647. @Override
  648. public void surfaceCreated (SurfaceHolder holder)
  649. {
  650. surfaceCreatedNative (nativeContext, holder);
  651. }
  652. @Override
  653. public void surfaceDestroyed (SurfaceHolder holder)
  654. {
  655. surfaceDestroyedNative (nativeContext, holder);
  656. }
  657. @Override
  658. protected void dispatchDraw (Canvas canvas)
  659. {
  660. super.dispatchDraw (canvas);
  661. dispatchDrawNative (nativeContext, canvas);
  662. }
  663. //==============================================================================
  664. @Override
  665. protected void onAttachedToWindow ()
  666. {
  667. super.onAttachedToWindow();
  668. getHolder().addCallback (this);
  669. }
  670. @Override
  671. protected void onDetachedFromWindow ()
  672. {
  673. super.onDetachedFromWindow();
  674. getHolder().removeCallback (this);
  675. }
  676. //==============================================================================
  677. private native void dispatchDrawNative (long nativeContextPtr, Canvas canvas);
  678. private native void surfaceCreatedNative (long nativeContextptr, SurfaceHolder holder);
  679. private native void surfaceDestroyedNative (long nativeContextptr, SurfaceHolder holder);
  680. private native void surfaceChangedNative (long nativeContextptr, SurfaceHolder holder,
  681. int format, int width, int height);
  682. }
  683. public NativeSurfaceView createNativeSurfaceView (long nativeSurfacePtr)
  684. {
  685. return new NativeSurfaceView (this, nativeSurfacePtr);
  686. }
  687. //==============================================================================
  688. public final int[] renderGlyph (char glyph, Paint paint, android.graphics.Matrix matrix, Rect bounds)
  689. {
  690. Path p = new Path();
  691. paint.getTextPath (String.valueOf (glyph), 0, 1, 0.0f, 0.0f, p);
  692. RectF boundsF = new RectF();
  693. p.computeBounds (boundsF, true);
  694. matrix.mapRect (boundsF);
  695. boundsF.roundOut (bounds);
  696. bounds.left--;
  697. bounds.right++;
  698. final int w = bounds.width();
  699. final int h = Math.max (1, bounds.height());
  700. Bitmap bm = Bitmap.createBitmap (w, h, Bitmap.Config.ARGB_8888);
  701. Canvas c = new Canvas (bm);
  702. matrix.postTranslate (-bounds.left, -bounds.top);
  703. c.setMatrix (matrix);
  704. c.drawPath (p, paint);
  705. final int sizeNeeded = w * h;
  706. if (cachedRenderArray.length < sizeNeeded)
  707. cachedRenderArray = new int [sizeNeeded];
  708. bm.getPixels (cachedRenderArray, 0, w, 0, 0, w, h);
  709. bm.recycle();
  710. return cachedRenderArray;
  711. }
  712. private int[] cachedRenderArray = new int [256];
  713. //==============================================================================
  714. public static class HTTPStream
  715. {
  716. public HTTPStream (HttpURLConnection connection_,
  717. int[] statusCode, StringBuffer responseHeaders) throws IOException
  718. {
  719. connection = connection_;
  720. try
  721. {
  722. inputStream = new BufferedInputStream (connection.getInputStream());
  723. }
  724. catch (IOException e)
  725. {
  726. if (connection.getResponseCode() < 400)
  727. throw e;
  728. }
  729. finally
  730. {
  731. statusCode[0] = connection.getResponseCode();
  732. }
  733. if (statusCode[0] >= 400)
  734. inputStream = connection.getErrorStream();
  735. else
  736. inputStream = connection.getInputStream();
  737. for (java.util.Map.Entry<String, java.util.List<String>> entry : connection.getHeaderFields().entrySet())
  738. if (entry.getKey() != null && entry.getValue() != null)
  739. responseHeaders.append (entry.getKey() + ": "
  740. + android.text.TextUtils.join (",", entry.getValue()) + "\n");
  741. }
  742. public final void release()
  743. {
  744. try
  745. {
  746. inputStream.close();
  747. }
  748. catch (IOException e)
  749. {}
  750. connection.disconnect();
  751. }
  752. public final int read (byte[] buffer, int numBytes)
  753. {
  754. int num = 0;
  755. try
  756. {
  757. num = inputStream.read (buffer, 0, numBytes);
  758. }
  759. catch (IOException e)
  760. {}
  761. if (num > 0)
  762. position += num;
  763. return num;
  764. }
  765. public final long getPosition() { return position; }
  766. public final long getTotalLength() { return -1; }
  767. public final boolean isExhausted() { return false; }
  768. public final boolean setPosition (long newPos) { return false; }
  769. private HttpURLConnection connection;
  770. private InputStream inputStream;
  771. private long position;
  772. }
  773. public static final HTTPStream createHTTPStream (String address, boolean isPost, byte[] postData,
  774. String headers, int timeOutMs, int[] statusCode,
  775. StringBuffer responseHeaders, int numRedirectsToFollow,
  776. String httpRequestCmd)
  777. {
  778. // timeout parameter of zero for HttpUrlConnection is a blocking connect (negative value for juce::URL)
  779. if (timeOutMs < 0)
  780. timeOutMs = 0;
  781. else if (timeOutMs == 0)
  782. timeOutMs = 30000;
  783. // headers - if not empty, this string is appended onto the headers that are used for the request. It must therefore be a valid set of HTML header directives, separated by newlines.
  784. // So convert headers string to an array, with an element for each line
  785. String headerLines[] = headers.split("\\n");
  786. for (;;)
  787. {
  788. try
  789. {
  790. HttpURLConnection connection = (HttpURLConnection) (new URL(address).openConnection());
  791. if (connection != null)
  792. {
  793. try
  794. {
  795. connection.setInstanceFollowRedirects (false);
  796. connection.setConnectTimeout (timeOutMs);
  797. connection.setReadTimeout (timeOutMs);
  798. // Set request headers
  799. for (int i = 0; i < headerLines.length; ++i)
  800. {
  801. int pos = headerLines[i].indexOf (":");
  802. if (pos > 0 && pos < headerLines[i].length())
  803. {
  804. String field = headerLines[i].substring (0, pos);
  805. String value = headerLines[i].substring (pos + 1);
  806. if (value.length() > 0)
  807. connection.setRequestProperty (field, value);
  808. }
  809. }
  810. connection.setRequestMethod (httpRequestCmd);
  811. if (isPost)
  812. {
  813. connection.setDoOutput (true);
  814. if (postData != null)
  815. {
  816. OutputStream out = connection.getOutputStream();
  817. out.write(postData);
  818. out.flush();
  819. }
  820. }
  821. HTTPStream httpStream = new HTTPStream (connection, statusCode, responseHeaders);
  822. // Process redirect & continue as necessary
  823. int status = statusCode[0];
  824. if (--numRedirectsToFollow >= 0
  825. && (status == 301 || status == 302 || status == 303 || status == 307))
  826. {
  827. // Assumes only one occurrence of "Location"
  828. int pos1 = responseHeaders.indexOf ("Location:") + 10;
  829. int pos2 = responseHeaders.indexOf ("\n", pos1);
  830. if (pos2 > pos1)
  831. {
  832. String newLocation = responseHeaders.substring(pos1, pos2);
  833. // Handle newLocation whether it's absolute or relative
  834. URL baseUrl = new URL (address);
  835. URL newUrl = new URL (baseUrl, newLocation);
  836. String transformedNewLocation = newUrl.toString();
  837. if (transformedNewLocation != address)
  838. {
  839. address = transformedNewLocation;
  840. // Clear responseHeaders before next iteration
  841. responseHeaders.delete (0, responseHeaders.length());
  842. continue;
  843. }
  844. }
  845. }
  846. return httpStream;
  847. }
  848. catch (Throwable e)
  849. {
  850. connection.disconnect();
  851. }
  852. }
  853. }
  854. catch (Throwable e) {}
  855. return null;
  856. }
  857. }
  858. public final void launchURL (String url)
  859. {
  860. startActivity (new Intent (Intent.ACTION_VIEW, Uri.parse (url)));
  861. }
  862. public static final String getLocaleValue (boolean isRegion)
  863. {
  864. java.util.Locale locale = java.util.Locale.getDefault();
  865. return isRegion ? locale.getDisplayCountry (java.util.Locale.US)
  866. : locale.getDisplayLanguage (java.util.Locale.US);
  867. }
  868. private static final String getFileLocation (String type)
  869. {
  870. return Environment.getExternalStoragePublicDirectory (type).getAbsolutePath();
  871. }
  872. public static final String getDocumentsFolder() { return Environment.getDataDirectory().getAbsolutePath(); }
  873. public static final String getPicturesFolder() { return getFileLocation (Environment.DIRECTORY_PICTURES); }
  874. public static final String getMusicFolder() { return getFileLocation (Environment.DIRECTORY_MUSIC); }
  875. public static final String getMoviesFolder() { return getFileLocation (Environment.DIRECTORY_MOVIES); }
  876. public static final String getDownloadsFolder() { return getFileLocation (Environment.DIRECTORY_DOWNLOADS); }
  877. //==============================================================================
  878. private final class SingleMediaScanner implements MediaScannerConnectionClient
  879. {
  880. public SingleMediaScanner (Context context, String filename)
  881. {
  882. file = filename;
  883. msc = new MediaScannerConnection (context, this);
  884. msc.connect();
  885. }
  886. @Override
  887. public void onMediaScannerConnected()
  888. {
  889. msc.scanFile (file, null);
  890. }
  891. @Override
  892. public void onScanCompleted (String path, Uri uri)
  893. {
  894. msc.disconnect();
  895. }
  896. private MediaScannerConnection msc;
  897. private String file;
  898. }
  899. public final void scanFile (String filename)
  900. {
  901. new SingleMediaScanner (this, filename);
  902. }
  903. public final Typeface getTypeFaceFromAsset (String assetName)
  904. {
  905. try
  906. {
  907. return Typeface.createFromAsset (this.getResources().getAssets(), assetName);
  908. }
  909. catch (Throwable e) {}
  910. return null;
  911. }
  912. final protected static char[] hexArray = "0123456789ABCDEF".toCharArray();
  913. public static String bytesToHex (byte[] bytes)
  914. {
  915. char[] hexChars = new char[bytes.length * 2];
  916. for (int j = 0; j < bytes.length; ++j)
  917. {
  918. int v = bytes[j] & 0xff;
  919. hexChars[j * 2] = hexArray[v >>> 4];
  920. hexChars[j * 2 + 1] = hexArray[v & 0x0f];
  921. }
  922. return new String (hexChars);
  923. }
  924. final private java.util.Map dataCache = new java.util.HashMap();
  925. synchronized private final File getDataCacheFile (byte[] data)
  926. {
  927. try
  928. {
  929. java.security.MessageDigest digest = java.security.MessageDigest.getInstance ("MD5");
  930. digest.update (data);
  931. String key = bytesToHex (digest.digest());
  932. if (dataCache.containsKey (key))
  933. return (File) dataCache.get (key);
  934. File f = new File (this.getCacheDir(), "bindata_" + key);
  935. f.delete();
  936. FileOutputStream os = new FileOutputStream (f);
  937. os.write (data, 0, data.length);
  938. dataCache.put (key, f);
  939. return f;
  940. }
  941. catch (Throwable e) {}
  942. return null;
  943. }
  944. private final void clearDataCache()
  945. {
  946. java.util.Iterator it = dataCache.values().iterator();
  947. while (it.hasNext())
  948. {
  949. File f = (File) it.next();
  950. f.delete();
  951. }
  952. }
  953. public final Typeface getTypeFaceFromByteArray (byte[] data)
  954. {
  955. try
  956. {
  957. File f = getDataCacheFile (data);
  958. if (f != null)
  959. return Typeface.createFromFile (f);
  960. }
  961. catch (Exception e)
  962. {
  963. Log.e ("JUCE", e.toString());
  964. }
  965. return null;
  966. }
  967. public final int getAndroidSDKVersion()
  968. {
  969. return android.os.Build.VERSION.SDK_INT;
  970. }
  971. public final String audioManagerGetProperty (String property)
  972. {
  973. Object obj = getSystemService (AUDIO_SERVICE);
  974. if (obj == null)
  975. return null;
  976. java.lang.reflect.Method method;
  977. try
  978. {
  979. method = obj.getClass().getMethod ("getProperty", String.class);
  980. }
  981. catch (SecurityException e) { return null; }
  982. catch (NoSuchMethodException e) { return null; }
  983. if (method == null)
  984. return null;
  985. try
  986. {
  987. return (String) method.invoke (obj, property);
  988. }
  989. catch (java.lang.IllegalArgumentException e) {}
  990. catch (java.lang.IllegalAccessException e) {}
  991. catch (java.lang.reflect.InvocationTargetException e) {}
  992. return null;
  993. }
  994. public final int setCurrentThreadPriority (int priority)
  995. {
  996. android.os.Process.setThreadPriority (android.os.Process.myTid(), priority);
  997. return android.os.Process.getThreadPriority (android.os.Process.myTid());
  998. }
  999. public final boolean hasSystemFeature (String property)
  1000. {
  1001. return getPackageManager().hasSystemFeature (property);
  1002. }
  1003. private static class JuceThread extends Thread
  1004. {
  1005. public JuceThread (long host, String threadName, long threadStackSize)
  1006. {
  1007. super (null, null, threadName, threadStackSize);
  1008. _this = host;
  1009. }
  1010. public void run()
  1011. {
  1012. runThread(_this);
  1013. }
  1014. private native void runThread (long host);
  1015. private long _this;
  1016. }
  1017. public final Thread createNewThread(long host, String threadName, long threadStackSize)
  1018. {
  1019. return new JuceThread(host, threadName, threadStackSize);
  1020. }
  1021. }