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.

901 lines
30KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2013 - Raw Material Software Ltd.
  5. Permission is granted to use this software under the terms of either:
  6. a) the GPL v2 (or any later version)
  7. b) the Affero GPL v3
  8. Details of these licenses can be found at: www.gnu.org/licenses
  9. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  11. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  12. ------------------------------------------------------------------------------
  13. To release a closed-source product which uses JUCE, commercial licenses are
  14. available: visit www.juce.com for more information.
  15. ==============================================================================
  16. */
  17. package com.juce;
  18. import android.app.Activity;
  19. import android.app.AlertDialog;
  20. import android.content.DialogInterface;
  21. import android.content.Context;
  22. import android.content.Intent;
  23. import android.content.res.Configuration;
  24. import android.net.Uri;
  25. import android.os.Bundle;
  26. import android.view.*;
  27. import android.view.inputmethod.BaseInputConnection;
  28. import android.view.inputmethod.EditorInfo;
  29. import android.view.inputmethod.InputConnection;
  30. import android.view.inputmethod.InputMethodManager;
  31. import android.graphics.*;
  32. import android.opengl.*;
  33. import android.text.ClipboardManager;
  34. import android.text.InputType;
  35. import android.util.DisplayMetrics;
  36. import android.util.Log;
  37. import java.io.*;
  38. import java.net.URL;
  39. import java.net.HttpURLConnection;
  40. import javax.microedition.khronos.egl.EGLConfig;
  41. import javax.microedition.khronos.opengles.GL10;
  42. import android.media.AudioManager;
  43. import android.media.MediaScannerConnection;
  44. import android.media.MediaScannerConnection.MediaScannerConnectionClient;
  45. //==============================================================================
  46. public class JuceAppActivity extends Activity
  47. {
  48. //==============================================================================
  49. static
  50. {
  51. System.loadLibrary ("juce_jni");
  52. }
  53. @Override
  54. public void onCreate (Bundle savedInstanceState)
  55. {
  56. super.onCreate (savedInstanceState);
  57. viewHolder = new ViewHolder (this);
  58. setContentView (viewHolder);
  59. setVolumeControlStream (AudioManager.STREAM_MUSIC);
  60. }
  61. @Override
  62. protected void onDestroy()
  63. {
  64. quitApp();
  65. super.onDestroy();
  66. clearDataCache();
  67. }
  68. @Override
  69. protected void onPause()
  70. {
  71. if (viewHolder != null)
  72. viewHolder.onPause();
  73. suspendApp();
  74. super.onPause();
  75. }
  76. @Override
  77. protected void onResume()
  78. {
  79. super.onResume();
  80. if (viewHolder != null)
  81. viewHolder.onResume();
  82. resumeApp();
  83. }
  84. @Override
  85. public void onConfigurationChanged (Configuration cfg)
  86. {
  87. super.onConfigurationChanged (cfg);
  88. setContentView (viewHolder);
  89. }
  90. private void callAppLauncher()
  91. {
  92. launchApp (getApplicationInfo().publicSourceDir,
  93. getApplicationInfo().dataDir);
  94. }
  95. //==============================================================================
  96. private native void launchApp (String appFile, String appDataDir);
  97. private native void quitApp();
  98. private native void suspendApp();
  99. private native void resumeApp();
  100. private native void setScreenSize (int screenWidth, int screenHeight, int dpi);
  101. //==============================================================================
  102. public native void deliverMessage (long value);
  103. private android.os.Handler messageHandler = new android.os.Handler();
  104. public final void postMessage (long value)
  105. {
  106. messageHandler.post (new MessageCallback (value));
  107. }
  108. private final class MessageCallback implements Runnable
  109. {
  110. public MessageCallback (long value_) { value = value_; }
  111. public final void run() { deliverMessage (value); }
  112. private long value;
  113. }
  114. //==============================================================================
  115. private ViewHolder viewHolder;
  116. public final ComponentPeerView createNewView (boolean opaque, long host)
  117. {
  118. ComponentPeerView v = new ComponentPeerView (this, opaque, host);
  119. viewHolder.addView (v);
  120. return v;
  121. }
  122. public final void deleteView (ComponentPeerView view)
  123. {
  124. ViewGroup group = (ViewGroup) (view.getParent());
  125. if (group != null)
  126. group.removeView (view);
  127. }
  128. public final void deleteOpenGLView (OpenGLView view)
  129. {
  130. ViewGroup group = (ViewGroup) (view.getParent());
  131. if (group != null)
  132. group.removeView (view);
  133. }
  134. final class ViewHolder extends ViewGroup
  135. {
  136. public ViewHolder (Context context)
  137. {
  138. super (context);
  139. setDescendantFocusability (ViewGroup.FOCUS_AFTER_DESCENDANTS);
  140. setFocusable (false);
  141. }
  142. protected final void onLayout (boolean changed, int left, int top, int right, int bottom)
  143. {
  144. setScreenSize (getWidth(), getHeight(), getDPI());
  145. if (isFirstResize)
  146. {
  147. isFirstResize = false;
  148. callAppLauncher();
  149. }
  150. }
  151. public final void onPause()
  152. {
  153. for (int i = getChildCount(); --i >= 0;)
  154. {
  155. View v = getChildAt (i);
  156. if (v instanceof ComponentPeerView)
  157. ((ComponentPeerView) v).onPause();
  158. }
  159. }
  160. public final void onResume()
  161. {
  162. for (int i = getChildCount(); --i >= 0;)
  163. {
  164. View v = getChildAt (i);
  165. if (v instanceof ComponentPeerView)
  166. ((ComponentPeerView) v).onResume();
  167. }
  168. }
  169. private final int getDPI()
  170. {
  171. DisplayMetrics metrics = new DisplayMetrics();
  172. getWindowManager().getDefaultDisplay().getMetrics (metrics);
  173. return metrics.densityDpi;
  174. }
  175. private boolean isFirstResize = true;
  176. }
  177. public final void excludeClipRegion (android.graphics.Canvas canvas, float left, float top, float right, float bottom)
  178. {
  179. canvas.clipRect (left, top, right, bottom, android.graphics.Region.Op.DIFFERENCE);
  180. }
  181. //==============================================================================
  182. public final String getClipboardContent()
  183. {
  184. ClipboardManager clipboard = (ClipboardManager) getSystemService (CLIPBOARD_SERVICE);
  185. return clipboard.getText().toString();
  186. }
  187. public final void setClipboardContent (String newText)
  188. {
  189. ClipboardManager clipboard = (ClipboardManager) getSystemService (CLIPBOARD_SERVICE);
  190. clipboard.setText (newText);
  191. }
  192. //==============================================================================
  193. public final void showMessageBox (String title, String message, final long callback)
  194. {
  195. AlertDialog.Builder builder = new AlertDialog.Builder (this);
  196. builder.setTitle (title)
  197. .setMessage (message)
  198. .setCancelable (true)
  199. .setPositiveButton ("OK", new DialogInterface.OnClickListener()
  200. {
  201. public void onClick (DialogInterface dialog, int id)
  202. {
  203. dialog.cancel();
  204. JuceAppActivity.this.alertDismissed (callback, 0);
  205. }
  206. });
  207. builder.create().show();
  208. }
  209. public final void showOkCancelBox (String title, String message, final long callback)
  210. {
  211. AlertDialog.Builder builder = new AlertDialog.Builder (this);
  212. builder.setTitle (title)
  213. .setMessage (message)
  214. .setCancelable (true)
  215. .setPositiveButton ("OK", new DialogInterface.OnClickListener()
  216. {
  217. public void onClick (DialogInterface dialog, int id)
  218. {
  219. dialog.cancel();
  220. JuceAppActivity.this.alertDismissed (callback, 1);
  221. }
  222. })
  223. .setNegativeButton ("Cancel", new DialogInterface.OnClickListener()
  224. {
  225. public void onClick (DialogInterface dialog, int id)
  226. {
  227. dialog.cancel();
  228. JuceAppActivity.this.alertDismissed (callback, 0);
  229. }
  230. });
  231. builder.create().show();
  232. }
  233. public final void showYesNoCancelBox (String title, String message, final long callback)
  234. {
  235. AlertDialog.Builder builder = new AlertDialog.Builder (this);
  236. builder.setTitle (title)
  237. .setMessage (message)
  238. .setCancelable (true)
  239. .setPositiveButton ("Yes", new DialogInterface.OnClickListener()
  240. {
  241. public void onClick (DialogInterface dialog, int id)
  242. {
  243. dialog.cancel();
  244. JuceAppActivity.this.alertDismissed (callback, 1);
  245. }
  246. })
  247. .setNegativeButton ("No", new DialogInterface.OnClickListener()
  248. {
  249. public void onClick (DialogInterface dialog, int id)
  250. {
  251. dialog.cancel();
  252. JuceAppActivity.this.alertDismissed (callback, 2);
  253. }
  254. })
  255. .setNeutralButton ("Cancel", new DialogInterface.OnClickListener()
  256. {
  257. public void onClick (DialogInterface dialog, int id)
  258. {
  259. dialog.cancel();
  260. JuceAppActivity.this.alertDismissed (callback, 0);
  261. }
  262. });
  263. builder.create().show();
  264. }
  265. public native void alertDismissed (long callback, int id);
  266. //==============================================================================
  267. public final class ComponentPeerView extends ViewGroup
  268. implements View.OnFocusChangeListener
  269. {
  270. public ComponentPeerView (Context context, boolean opaque_, long host)
  271. {
  272. super (context);
  273. this.host = host;
  274. setWillNotDraw (false);
  275. opaque = opaque_;
  276. setFocusable (true);
  277. setFocusableInTouchMode (true);
  278. setOnFocusChangeListener (this);
  279. requestFocus();
  280. }
  281. //==============================================================================
  282. private native void handlePaint (long host, Canvas canvas);
  283. @Override
  284. public void onDraw (Canvas canvas)
  285. {
  286. handlePaint (host, canvas);
  287. }
  288. @Override
  289. public boolean isOpaque()
  290. {
  291. return opaque;
  292. }
  293. private boolean opaque;
  294. private long host;
  295. //==============================================================================
  296. private native void handleMouseDown (long host, int index, float x, float y, long time);
  297. private native void handleMouseDrag (long host, int index, float x, float y, long time);
  298. private native void handleMouseUp (long host, int index, float x, float y, long time);
  299. @Override
  300. public boolean onTouchEvent (MotionEvent event)
  301. {
  302. int action = event.getAction();
  303. long time = event.getEventTime();
  304. switch (action & MotionEvent.ACTION_MASK)
  305. {
  306. case MotionEvent.ACTION_DOWN:
  307. handleMouseDown (host, event.getPointerId(0), event.getX(), event.getY(), time);
  308. return true;
  309. case MotionEvent.ACTION_CANCEL:
  310. case MotionEvent.ACTION_UP:
  311. handleMouseUp (host, event.getPointerId(0), event.getX(), event.getY(), time);
  312. return true;
  313. case MotionEvent.ACTION_MOVE:
  314. {
  315. int n = event.getPointerCount();
  316. for (int i = 0; i < n; ++i)
  317. handleMouseDrag (host, event.getPointerId(i), event.getX(i), event.getY(i), time);
  318. return true;
  319. }
  320. case MotionEvent.ACTION_POINTER_UP:
  321. {
  322. int i = (action & MotionEvent.ACTION_POINTER_INDEX_MASK) >> MotionEvent.ACTION_POINTER_INDEX_SHIFT;
  323. handleMouseUp (host, event.getPointerId(i), event.getX(i), event.getY(i), time);
  324. return true;
  325. }
  326. case MotionEvent.ACTION_POINTER_DOWN:
  327. {
  328. int i = (action & MotionEvent.ACTION_POINTER_INDEX_MASK) >> MotionEvent.ACTION_POINTER_INDEX_SHIFT;
  329. handleMouseDown (host, event.getPointerId(i), event.getX(i), event.getY(i), time);
  330. return true;
  331. }
  332. default:
  333. break;
  334. }
  335. return false;
  336. }
  337. //==============================================================================
  338. private native void handleKeyDown (long host, int keycode, int textchar);
  339. private native void handleKeyUp (long host, int keycode, int textchar);
  340. public void showKeyboard (String type)
  341. {
  342. InputMethodManager imm = (InputMethodManager) getSystemService (Context.INPUT_METHOD_SERVICE);
  343. if (imm != null)
  344. {
  345. if (type.length() > 0)
  346. {
  347. imm.showSoftInput (this, android.view.inputmethod.InputMethodManager.SHOW_IMPLICIT);
  348. imm.setInputMethod (getWindowToken(), type);
  349. }
  350. else
  351. {
  352. imm.hideSoftInputFromWindow (getWindowToken(), 0);
  353. }
  354. }
  355. }
  356. @Override
  357. public boolean onKeyDown (int keyCode, KeyEvent event)
  358. {
  359. switch (keyCode)
  360. {
  361. case KeyEvent.KEYCODE_VOLUME_UP:
  362. case KeyEvent.KEYCODE_VOLUME_DOWN:
  363. return super.onKeyDown (keyCode, event);
  364. default: break;
  365. }
  366. handleKeyDown (host, keyCode, event.getUnicodeChar());
  367. return true;
  368. }
  369. @Override
  370. public boolean onKeyUp (int keyCode, KeyEvent event)
  371. {
  372. handleKeyUp (host, keyCode, event.getUnicodeChar());
  373. return true;
  374. }
  375. // this is here to make keyboard entry work on a Galaxy Tab2 10.1
  376. @Override
  377. public InputConnection onCreateInputConnection (EditorInfo outAttrs)
  378. {
  379. outAttrs.actionLabel = "";
  380. outAttrs.hintText = "";
  381. outAttrs.initialCapsMode = 0;
  382. outAttrs.initialSelEnd = outAttrs.initialSelStart = -1;
  383. outAttrs.label = "";
  384. outAttrs.imeOptions = EditorInfo.IME_ACTION_DONE | EditorInfo.IME_FLAG_NO_EXTRACT_UI;
  385. outAttrs.inputType = InputType.TYPE_NULL;
  386. return new BaseInputConnection (this, false);
  387. }
  388. //==============================================================================
  389. @Override
  390. protected void onSizeChanged (int w, int h, int oldw, int oldh)
  391. {
  392. super.onSizeChanged (w, h, oldw, oldh);
  393. viewSizeChanged (host);
  394. }
  395. @Override
  396. protected void onLayout (boolean changed, int left, int top, int right, int bottom)
  397. {
  398. for (int i = getChildCount(); --i >= 0;)
  399. requestTransparentRegion (getChildAt (i));
  400. }
  401. private native void viewSizeChanged (long host);
  402. @Override
  403. public void onFocusChange (View v, boolean hasFocus)
  404. {
  405. if (v == this)
  406. focusChanged (host, hasFocus);
  407. }
  408. private native void focusChanged (long host, boolean hasFocus);
  409. public void setViewName (String newName) {}
  410. public boolean isVisible() { return getVisibility() == VISIBLE; }
  411. public void setVisible (boolean b) { setVisibility (b ? VISIBLE : INVISIBLE); }
  412. public boolean containsPoint (int x, int y)
  413. {
  414. return true; //xxx needs to check overlapping views
  415. }
  416. public final void onPause()
  417. {
  418. for (int i = getChildCount(); --i >= 0;)
  419. {
  420. View v = getChildAt (i);
  421. if (v instanceof OpenGLView)
  422. ((OpenGLView) v).onPause();
  423. }
  424. }
  425. public final void onResume()
  426. {
  427. for (int i = getChildCount(); --i >= 0;)
  428. {
  429. View v = getChildAt (i);
  430. if (v instanceof OpenGLView)
  431. ((OpenGLView) v).onResume();
  432. }
  433. }
  434. public OpenGLView createGLView()
  435. {
  436. OpenGLView glView = new OpenGLView (getContext());
  437. addView (glView);
  438. return glView;
  439. }
  440. }
  441. //==============================================================================
  442. public final class OpenGLView extends GLSurfaceView
  443. implements GLSurfaceView.Renderer
  444. {
  445. OpenGLView (Context context)
  446. {
  447. super (context);
  448. setEGLContextClientVersion (2);
  449. setRenderer (this);
  450. setRenderMode (RENDERMODE_WHEN_DIRTY);
  451. }
  452. @Override
  453. public void onSurfaceCreated (GL10 unused, EGLConfig config)
  454. {
  455. contextCreated();
  456. }
  457. @Override
  458. public void onSurfaceChanged (GL10 unused, int width, int height)
  459. {
  460. contextChangedSize();
  461. }
  462. @Override
  463. public void onDrawFrame (GL10 unused)
  464. {
  465. render();
  466. }
  467. private native void contextCreated();
  468. private native void contextChangedSize();
  469. private native void render();
  470. }
  471. //==============================================================================
  472. public final int[] renderGlyph (char glyph, Paint paint, android.graphics.Matrix matrix, Rect bounds)
  473. {
  474. Path p = new Path();
  475. paint.getTextPath (String.valueOf (glyph), 0, 1, 0.0f, 0.0f, p);
  476. RectF boundsF = new RectF();
  477. p.computeBounds (boundsF, true);
  478. matrix.mapRect (boundsF);
  479. boundsF.roundOut (bounds);
  480. bounds.left--;
  481. bounds.right++;
  482. final int w = bounds.width();
  483. final int h = Math.max (1, bounds.height());
  484. Bitmap bm = Bitmap.createBitmap (w, h, Bitmap.Config.ARGB_8888);
  485. Canvas c = new Canvas (bm);
  486. matrix.postTranslate (-bounds.left, -bounds.top);
  487. c.setMatrix (matrix);
  488. c.drawPath (p, paint);
  489. final int sizeNeeded = w * h;
  490. if (cachedRenderArray.length < sizeNeeded)
  491. cachedRenderArray = new int [sizeNeeded];
  492. bm.getPixels (cachedRenderArray, 0, w, 0, 0, w, h);
  493. bm.recycle();
  494. return cachedRenderArray;
  495. }
  496. private int[] cachedRenderArray = new int [256];
  497. //==============================================================================
  498. public static class HTTPStream
  499. {
  500. public HTTPStream (HttpURLConnection connection_,
  501. int[] statusCode, StringBuffer responseHeaders) throws IOException
  502. {
  503. connection = connection_;
  504. try
  505. {
  506. inputStream = new BufferedInputStream (connection.getInputStream());
  507. }
  508. catch (IOException e)
  509. {
  510. if (connection.getResponseCode() < org.apache.http.HttpStatus.SC_BAD_REQUEST)
  511. throw e;
  512. }
  513. finally
  514. {
  515. statusCode[0] = connection.getResponseCode();
  516. }
  517. if (statusCode[0] >= org.apache.http.HttpStatus.SC_BAD_REQUEST)
  518. inputStream = connection.getErrorStream();
  519. else
  520. inputStream = connection.getInputStream();
  521. for (java.util.Map.Entry<String, java.util.List<String>> entry : connection.getHeaderFields().entrySet())
  522. if (entry.getKey() != null && entry.getValue() != null)
  523. responseHeaders.append (entry.getKey() + ": "
  524. + android.text.TextUtils.join (",", entry.getValue()) + "\n");
  525. }
  526. public final void release()
  527. {
  528. try
  529. {
  530. inputStream.close();
  531. }
  532. catch (IOException e)
  533. {}
  534. connection.disconnect();
  535. }
  536. public final int read (byte[] buffer, int numBytes)
  537. {
  538. int num = 0;
  539. try
  540. {
  541. num = inputStream.read (buffer, 0, numBytes);
  542. }
  543. catch (IOException e)
  544. {}
  545. if (num > 0)
  546. position += num;
  547. return num;
  548. }
  549. public final long getPosition() { return position; }
  550. public final long getTotalLength() { return -1; }
  551. public final boolean isExhausted() { return false; }
  552. public final boolean setPosition (long newPos) { return false; }
  553. private HttpURLConnection connection;
  554. private InputStream inputStream;
  555. private long position;
  556. }
  557. public static final HTTPStream createHTTPStream (String address,
  558. boolean isPost, byte[] postData, String headers,
  559. int timeOutMs, int[] statusCode,
  560. StringBuffer responseHeaders,
  561. int numRedirectsToFollow)
  562. {
  563. // timeout parameter of zero for HttpUrlConnection is a blocking connect (negative value for juce::URL)
  564. if (timeOutMs < 0)
  565. timeOutMs = 0;
  566. else if (timeOutMs == 0)
  567. timeOutMs = 30000;
  568. // 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.
  569. // So convert headers string to an array, with an element for each line
  570. String headerLines[] = headers.split("\\n");
  571. for (;;)
  572. {
  573. try
  574. {
  575. HttpURLConnection connection = (HttpURLConnection) (new URL(address).openConnection());
  576. if (connection != null)
  577. {
  578. try
  579. {
  580. connection.setInstanceFollowRedirects (false);
  581. connection.setConnectTimeout (timeOutMs);
  582. connection.setReadTimeout (timeOutMs);
  583. // Set request headers
  584. for (int i = 0; i < headerLines.length; ++i)
  585. {
  586. int pos = headerLines[i].indexOf (":");
  587. if (pos > 0 && pos < headerLines[i].length())
  588. {
  589. String field = headerLines[i].substring (0, pos);
  590. String value = headerLines[i].substring (pos + 1);
  591. if (value.length() > 0)
  592. connection.setRequestProperty (field, value);
  593. }
  594. }
  595. if (isPost)
  596. {
  597. connection.setRequestMethod ("POST");
  598. connection.setDoOutput (true);
  599. if (postData != null)
  600. {
  601. OutputStream out = connection.getOutputStream();
  602. out.write(postData);
  603. out.flush();
  604. }
  605. }
  606. HTTPStream httpStream = new HTTPStream (connection, statusCode, responseHeaders);
  607. // Process redirect & continue as necessary
  608. int status = statusCode[0];
  609. if (--numRedirectsToFollow >= 0
  610. && (status == 301 || status == 302 || status == 303 || status == 307))
  611. {
  612. // Assumes only one occurrence of "Location"
  613. int pos1 = responseHeaders.indexOf ("Location:") + 10;
  614. int pos2 = responseHeaders.indexOf ("\n", pos1);
  615. if (pos2 > pos1)
  616. {
  617. String newLocation = responseHeaders.substring(pos1, pos2);
  618. // Handle newLocation whether it's absolute or relative
  619. URL baseUrl = new URL (address);
  620. URL newUrl = new URL (baseUrl, newLocation);
  621. String transformedNewLocation = newUrl.toString();
  622. if (transformedNewLocation != address)
  623. {
  624. address = transformedNewLocation;
  625. // Clear responseHeaders before next iteration
  626. responseHeaders.delete (0, responseHeaders.length());
  627. continue;
  628. }
  629. }
  630. }
  631. return httpStream;
  632. }
  633. catch (Throwable e)
  634. {
  635. connection.disconnect();
  636. }
  637. }
  638. }
  639. catch (Throwable e) {}
  640. return null;
  641. }
  642. }
  643. public final void launchURL (String url)
  644. {
  645. startActivity (new Intent (Intent.ACTION_VIEW, Uri.parse (url)));
  646. }
  647. public static final String getLocaleValue (boolean isRegion)
  648. {
  649. java.util.Locale locale = java.util.Locale.getDefault();
  650. return isRegion ? locale.getDisplayCountry (java.util.Locale.US)
  651. : locale.getDisplayLanguage (java.util.Locale.US);
  652. }
  653. //==============================================================================
  654. private final class SingleMediaScanner implements MediaScannerConnectionClient
  655. {
  656. public SingleMediaScanner (Context context, String filename)
  657. {
  658. file = filename;
  659. msc = new MediaScannerConnection (context, this);
  660. msc.connect();
  661. }
  662. @Override
  663. public void onMediaScannerConnected()
  664. {
  665. msc.scanFile (file, null);
  666. }
  667. @Override
  668. public void onScanCompleted (String path, Uri uri)
  669. {
  670. msc.disconnect();
  671. }
  672. private MediaScannerConnection msc;
  673. private String file;
  674. }
  675. public final void scanFile (String filename)
  676. {
  677. new SingleMediaScanner (this, filename);
  678. }
  679. public final Typeface getTypeFaceFromAsset (String assetName)
  680. {
  681. try
  682. {
  683. return Typeface.createFromAsset (this.getResources().getAssets(), assetName);
  684. }
  685. catch (Throwable e) {}
  686. return null;
  687. }
  688. final protected static char[] hexArray = "0123456789ABCDEF".toCharArray();
  689. public static String bytesToHex (byte[] bytes)
  690. {
  691. char[] hexChars = new char[bytes.length * 2];
  692. for (int j = 0; j < bytes.length; ++j)
  693. {
  694. int v = bytes[j] & 0xff;
  695. hexChars[j * 2] = hexArray[v >>> 4];
  696. hexChars[j * 2 + 1] = hexArray[v & 0x0f];
  697. }
  698. return new String (hexChars);
  699. }
  700. final private java.util.Map dataCache = new java.util.HashMap();
  701. synchronized private final File getDataCacheFile (byte[] data)
  702. {
  703. try
  704. {
  705. java.security.MessageDigest digest = java.security.MessageDigest.getInstance ("MD5");
  706. digest.update (data);
  707. String key = bytesToHex (digest.digest());
  708. if (dataCache.containsKey (key))
  709. return (File) dataCache.get (key);
  710. File f = new File (this.getCacheDir(), "bindata_" + key);
  711. f.delete();
  712. FileOutputStream os = new FileOutputStream (f);
  713. os.write (data, 0, data.length);
  714. dataCache.put (key, f);
  715. return f;
  716. }
  717. catch (Throwable e) {}
  718. return null;
  719. }
  720. private final void clearDataCache()
  721. {
  722. java.util.Iterator it = dataCache.values().iterator();
  723. while (it.hasNext())
  724. {
  725. File f = (File) it.next();
  726. f.delete();
  727. }
  728. }
  729. public final Typeface getTypeFaceFromByteArray (byte[] data)
  730. {
  731. try
  732. {
  733. File f = getDataCacheFile (data);
  734. if (f != null)
  735. return Typeface.createFromFile (f);
  736. }
  737. catch (Exception e)
  738. {
  739. Log.e ("JUCE", e.toString());
  740. }
  741. return null;
  742. }
  743. }