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.

1807 lines
64KB

  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. The code included in this file is provided under the terms of the ISC license
  8. http://www.isc.org/downloads/software-support-policy/isc-license. Permission
  9. To use, copy, modify, and/or distribute this software for any purpose with or
  10. without fee is hereby granted provided that the above copyright notice and
  11. this permission notice appear in all copies.
  12. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  13. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  14. DISCLAIMED.
  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.content.pm.PackageInfo;
  25. import android.content.pm.PackageManager;
  26. $$JuceAndroidCameraImports$$ // If you get an error here, you need to re-save your project with the Projucer!
  27. $$JuceAndroidVideoImports$$ // If you get an error here, you need to re-save your project with the Projucer!
  28. import android.net.http.SslError;
  29. import android.net.Uri;
  30. import android.os.Bundle;
  31. import android.os.Looper;
  32. import android.os.Handler;
  33. import android.os.Message;
  34. import android.os.ParcelUuid;
  35. import android.os.Environment;
  36. import android.view.*;
  37. import android.view.inputmethod.BaseInputConnection;
  38. import android.view.inputmethod.EditorInfo;
  39. import android.view.inputmethod.InputConnection;
  40. import android.view.inputmethod.InputMethodManager;
  41. import android.graphics.*;
  42. import android.text.ClipboardManager;
  43. import android.text.InputType;
  44. import android.util.DisplayMetrics;
  45. import android.util.Log;
  46. import android.util.Pair;
  47. import android.webkit.SslErrorHandler;
  48. import android.webkit.WebChromeClient;
  49. $$JuceAndroidWebViewImports$$ // If you get an error here, you need to re-save your project with the Projucer!
  50. import android.webkit.WebView;
  51. import android.webkit.WebViewClient;
  52. import java.lang.Runnable;
  53. import java.lang.ref.WeakReference;
  54. import java.lang.reflect.*;
  55. import java.util.*;
  56. import java.io.*;
  57. import java.net.URL;
  58. import java.net.HttpURLConnection;
  59. import android.media.AudioManager;
  60. import android.Manifest;
  61. import java.util.concurrent.CancellationException;
  62. import java.util.concurrent.Future;
  63. import java.util.concurrent.Executors;
  64. import java.util.concurrent.ExecutorService;
  65. import java.util.concurrent.ExecutionException;
  66. import java.util.concurrent.TimeUnit;
  67. import java.util.concurrent.Callable;
  68. import java.util.concurrent.TimeoutException;
  69. import java.util.concurrent.locks.ReentrantLock;
  70. import java.util.concurrent.atomic.*;
  71. $$JuceAndroidMidiImports$$ // If you get an error here, you need to re-save your project with the Projucer!
  72. //==============================================================================
  73. public class JuceAppActivity extends $$JuceAppActivityBaseClass$$
  74. {
  75. //==============================================================================
  76. static
  77. {
  78. System.loadLibrary ("juce_jni");
  79. }
  80. //==============================================================================
  81. public boolean isPermissionDeclaredInManifest (int permissionID)
  82. {
  83. return isPermissionDeclaredInManifest (getAndroidPermissionName (permissionID));
  84. }
  85. public boolean isPermissionDeclaredInManifest (String permissionToCheck)
  86. {
  87. try
  88. {
  89. PackageInfo info = getPackageManager().getPackageInfo(getApplicationContext().getPackageName(), PackageManager.GET_PERMISSIONS);
  90. if (info.requestedPermissions != null)
  91. for (String permission : info.requestedPermissions)
  92. if (permission.equals (permissionToCheck))
  93. return true;
  94. }
  95. catch (PackageManager.NameNotFoundException e)
  96. {
  97. Log.d ("JUCE", "isPermissionDeclaredInManifest: PackageManager.NameNotFoundException = " + e.toString());
  98. }
  99. Log.d ("JUCE", "isPermissionDeclaredInManifest: could not find requested permission " + permissionToCheck);
  100. return false;
  101. }
  102. //==============================================================================
  103. // these have to match the values of enum PermissionID in C++ class RuntimePermissions:
  104. private static final int JUCE_PERMISSIONS_RECORD_AUDIO = 1;
  105. private static final int JUCE_PERMISSIONS_BLUETOOTH_MIDI = 2;
  106. private static final int JUCE_PERMISSIONS_READ_EXTERNAL_STORAGE = 3;
  107. private static final int JUCE_PERMISSIONS_WRITE_EXTERNAL_STORAGE = 4;
  108. private static final int JUCE_PERMISSIONS_CAMERA = 5;
  109. private static String getAndroidPermissionName (int permissionID)
  110. {
  111. switch (permissionID)
  112. {
  113. case JUCE_PERMISSIONS_RECORD_AUDIO: return Manifest.permission.RECORD_AUDIO;
  114. case JUCE_PERMISSIONS_BLUETOOTH_MIDI: return Manifest.permission.ACCESS_COARSE_LOCATION;
  115. // use string value as this is not defined in SDKs < 16
  116. case JUCE_PERMISSIONS_READ_EXTERNAL_STORAGE: return "android.permission.READ_EXTERNAL_STORAGE";
  117. case JUCE_PERMISSIONS_WRITE_EXTERNAL_STORAGE: return Manifest.permission.WRITE_EXTERNAL_STORAGE;
  118. case JUCE_PERMISSIONS_CAMERA: return Manifest.permission.CAMERA;
  119. }
  120. // unknown permission ID!
  121. assert false;
  122. return new String();
  123. }
  124. public boolean isPermissionGranted (int permissionID)
  125. {
  126. return getApplicationContext().checkCallingOrSelfPermission (getAndroidPermissionName (permissionID)) == PackageManager.PERMISSION_GRANTED;
  127. }
  128. private Map<Integer, Long> permissionCallbackPtrMap;
  129. public void requestRuntimePermission (int permissionID, long ptrToCallback)
  130. {
  131. String permissionName = getAndroidPermissionName (permissionID);
  132. if (getApplicationContext().checkCallingOrSelfPermission (permissionName) != PackageManager.PERMISSION_GRANTED)
  133. {
  134. // remember callbackPtr, request permissions, and let onRequestPermissionResult call callback asynchronously
  135. permissionCallbackPtrMap.put (permissionID, ptrToCallback);
  136. requestPermissionsCompat (new String[]{permissionName}, permissionID);
  137. }
  138. else
  139. {
  140. // permissions were already granted before, we can call callback directly
  141. androidRuntimePermissionsCallback (true, ptrToCallback);
  142. }
  143. }
  144. private native void androidRuntimePermissionsCallback (boolean permissionWasGranted, long ptrToCallback);
  145. $$JuceAndroidRuntimePermissionsCode$$ // If you get an error here, you need to re-save your project with the Projucer!
  146. //==============================================================================
  147. public interface JuceMidiPort
  148. {
  149. boolean isInputPort();
  150. // start, stop does nothing on an output port
  151. void start();
  152. void stop();
  153. void close();
  154. // send will do nothing on an input port
  155. void sendMidi (byte[] msg, int offset, int count);
  156. }
  157. //==============================================================================
  158. $$JuceAndroidMidiCode$$ // If you get an error here, you need to re-save your project with the Projucer!
  159. //==============================================================================
  160. @Override
  161. public void onCreate (Bundle savedInstanceState)
  162. {
  163. super.onCreate (savedInstanceState);
  164. isScreenSaverEnabled = true;
  165. hideActionBar();
  166. viewHolder = new ViewHolder (this);
  167. setContentView (viewHolder);
  168. setVolumeControlStream (AudioManager.STREAM_MUSIC);
  169. permissionCallbackPtrMap = new HashMap<Integer, Long>();
  170. appPausedResumedListeners = new HashMap<Long, AppPausedResumedListener>();
  171. }
  172. @Override
  173. protected void onDestroy()
  174. {
  175. quitApp();
  176. super.onDestroy();
  177. clearDataCache();
  178. }
  179. @Override
  180. protected void onPause()
  181. {
  182. suspendApp();
  183. Long[] keys = appPausedResumedListeners.keySet().toArray (new Long[appPausedResumedListeners.keySet().size()]);
  184. for (Long k : keys)
  185. appPausedResumedListeners.get (k).appPaused();
  186. try
  187. {
  188. Thread.sleep (1000); // This is a bit of a hack to avoid some hard-to-track-down
  189. // openGL glitches when pausing/resuming apps..
  190. } catch (InterruptedException e) {}
  191. super.onPause();
  192. }
  193. @Override
  194. protected void onResume()
  195. {
  196. super.onResume();
  197. resumeApp();
  198. Long[] keys = appPausedResumedListeners.keySet().toArray (new Long[appPausedResumedListeners.keySet().size()]);
  199. for (Long k : keys)
  200. appPausedResumedListeners.get (k).appResumed();
  201. }
  202. @Override
  203. public void onConfigurationChanged (Configuration cfg)
  204. {
  205. super.onConfigurationChanged (cfg);
  206. setContentView (viewHolder);
  207. }
  208. private void callAppLauncher()
  209. {
  210. launchApp (getApplicationInfo().publicSourceDir,
  211. getApplicationInfo().dataDir);
  212. }
  213. // Need to override this as the default implementation always finishes the activity.
  214. @Override
  215. public void onBackPressed()
  216. {
  217. ComponentPeerView focusedView = getViewWithFocusOrDefaultView();
  218. if (focusedView == null)
  219. return;
  220. focusedView.backButtonPressed();
  221. }
  222. private ComponentPeerView getViewWithFocusOrDefaultView()
  223. {
  224. for (int i = 0; i < viewHolder.getChildCount(); ++i)
  225. {
  226. if (viewHolder.getChildAt (i).hasFocus())
  227. return (ComponentPeerView) viewHolder.getChildAt (i);
  228. }
  229. if (viewHolder.getChildCount() > 0)
  230. return (ComponentPeerView) viewHolder.getChildAt (0);
  231. return null;
  232. }
  233. //==============================================================================
  234. private void hideActionBar()
  235. {
  236. // get "getActionBar" method
  237. java.lang.reflect.Method getActionBarMethod = null;
  238. try
  239. {
  240. getActionBarMethod = this.getClass().getMethod ("getActionBar");
  241. }
  242. catch (SecurityException e) { return; }
  243. catch (NoSuchMethodException e) { return; }
  244. if (getActionBarMethod == null) return;
  245. // invoke "getActionBar" method
  246. Object actionBar = null;
  247. try
  248. {
  249. actionBar = getActionBarMethod.invoke (this);
  250. }
  251. catch (java.lang.IllegalArgumentException e) { return; }
  252. catch (java.lang.IllegalAccessException e) { return; }
  253. catch (java.lang.reflect.InvocationTargetException e) { return; }
  254. if (actionBar == null) return;
  255. // get "hide" method
  256. java.lang.reflect.Method actionBarHideMethod = null;
  257. try
  258. {
  259. actionBarHideMethod = actionBar.getClass().getMethod ("hide");
  260. }
  261. catch (SecurityException e) { return; }
  262. catch (NoSuchMethodException e) { return; }
  263. if (actionBarHideMethod == null) return;
  264. // invoke "hide" method
  265. try
  266. {
  267. actionBarHideMethod.invoke (actionBar);
  268. }
  269. catch (java.lang.IllegalArgumentException e) {}
  270. catch (java.lang.IllegalAccessException e) {}
  271. catch (java.lang.reflect.InvocationTargetException e) {}
  272. }
  273. void requestPermissionsCompat (String[] permissions, int requestCode)
  274. {
  275. Method requestPermissionsMethod = null;
  276. try
  277. {
  278. requestPermissionsMethod = this.getClass().getMethod ("requestPermissions",
  279. String[].class, int.class);
  280. }
  281. catch (SecurityException e) { return; }
  282. catch (NoSuchMethodException e) { return; }
  283. if (requestPermissionsMethod == null) return;
  284. try
  285. {
  286. requestPermissionsMethod.invoke (this, permissions, requestCode);
  287. }
  288. catch (java.lang.IllegalArgumentException e) {}
  289. catch (java.lang.IllegalAccessException e) {}
  290. catch (java.lang.reflect.InvocationTargetException e) {}
  291. }
  292. //==============================================================================
  293. private native void launchApp (String appFile, String appDataDir);
  294. private native void quitApp();
  295. private native void suspendApp();
  296. private native void resumeApp();
  297. private native void setScreenSize (int screenWidth, int screenHeight, int dpi);
  298. private native void appActivityResult (int requestCode, int resultCode, Intent data);
  299. private native void appNewIntent (Intent intent);
  300. //==============================================================================
  301. private ViewHolder viewHolder;
  302. private MidiDeviceManager midiDeviceManager = null;
  303. private BluetoothManager bluetoothManager = null;
  304. private boolean isScreenSaverEnabled;
  305. private java.util.Timer keepAliveTimer;
  306. public final ComponentPeerView createNewView (boolean opaque, long host)
  307. {
  308. ComponentPeerView v = new ComponentPeerView (this, opaque, host);
  309. viewHolder.addView (v);
  310. addAppPausedResumedListener (v, host);
  311. return v;
  312. }
  313. public final void deleteView (ComponentPeerView view)
  314. {
  315. removeAppPausedResumedListener (view, view.host);
  316. view.host = 0;
  317. ViewGroup group = (ViewGroup) (view.getParent());
  318. if (group != null)
  319. group.removeView (view);
  320. }
  321. public final void deleteNativeSurfaceView (NativeSurfaceView view)
  322. {
  323. ViewGroup group = (ViewGroup) (view.getParent());
  324. if (group != null)
  325. group.removeView (view);
  326. }
  327. final class ViewHolder extends ViewGroup
  328. {
  329. public ViewHolder (Context context)
  330. {
  331. super (context);
  332. setDescendantFocusability (ViewGroup.FOCUS_AFTER_DESCENDANTS);
  333. setFocusable (false);
  334. }
  335. protected final void onLayout (boolean changed, int left, int top, int right, int bottom)
  336. {
  337. setScreenSize (getWidth(), getHeight(), getDPI());
  338. if (isFirstResize)
  339. {
  340. isFirstResize = false;
  341. callAppLauncher();
  342. }
  343. }
  344. private final int getDPI()
  345. {
  346. DisplayMetrics metrics = new DisplayMetrics();
  347. getWindowManager().getDefaultDisplay().getMetrics (metrics);
  348. return metrics.densityDpi;
  349. }
  350. private boolean isFirstResize = true;
  351. }
  352. public final void excludeClipRegion (android.graphics.Canvas canvas, float left, float top, float right, float bottom)
  353. {
  354. canvas.clipRect (left, top, right, bottom, android.graphics.Region.Op.DIFFERENCE);
  355. }
  356. //==============================================================================
  357. public final void setScreenSaver (boolean enabled)
  358. {
  359. if (isScreenSaverEnabled != enabled)
  360. {
  361. isScreenSaverEnabled = enabled;
  362. if (keepAliveTimer != null)
  363. {
  364. keepAliveTimer.cancel();
  365. keepAliveTimer = null;
  366. }
  367. if (enabled)
  368. {
  369. getWindow().clearFlags (WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
  370. }
  371. else
  372. {
  373. getWindow().addFlags (WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
  374. // If no user input is received after about 3 seconds, the OS will lower the
  375. // task's priority, so this timer forces it to be kept active.
  376. keepAliveTimer = new java.util.Timer();
  377. keepAliveTimer.scheduleAtFixedRate (new TimerTask()
  378. {
  379. @Override
  380. public void run()
  381. {
  382. android.app.Instrumentation instrumentation = new android.app.Instrumentation();
  383. try
  384. {
  385. instrumentation.sendKeyDownUpSync (KeyEvent.KEYCODE_UNKNOWN);
  386. }
  387. catch (Exception e)
  388. {
  389. }
  390. }
  391. }, 2000, 2000);
  392. }
  393. }
  394. }
  395. public final boolean getScreenSaver()
  396. {
  397. return isScreenSaverEnabled;
  398. }
  399. //==============================================================================
  400. public final String getClipboardContent()
  401. {
  402. ClipboardManager clipboard = (ClipboardManager) getSystemService (CLIPBOARD_SERVICE);
  403. CharSequence content = clipboard.getText();
  404. return content != null ? content.toString() : new String();
  405. }
  406. public final void setClipboardContent (String newText)
  407. {
  408. ClipboardManager clipboard = (ClipboardManager) getSystemService (CLIPBOARD_SERVICE);
  409. clipboard.setText (newText);
  410. }
  411. //==============================================================================
  412. public final void showMessageBox (String title, String message, final long callback)
  413. {
  414. AlertDialog.Builder builder = new AlertDialog.Builder (this);
  415. builder.setTitle (title)
  416. .setMessage (message)
  417. .setCancelable (true)
  418. .setOnCancelListener (new DialogInterface.OnCancelListener()
  419. {
  420. public void onCancel (DialogInterface dialog)
  421. {
  422. JuceAppActivity.this.alertDismissed (callback, 0);
  423. }
  424. })
  425. .setPositiveButton ("OK", new DialogInterface.OnClickListener()
  426. {
  427. public void onClick (DialogInterface dialog, int id)
  428. {
  429. dialog.dismiss();
  430. JuceAppActivity.this.alertDismissed (callback, 0);
  431. }
  432. });
  433. builder.create().show();
  434. }
  435. public final void showOkCancelBox (String title, String message, final long callback,
  436. String okButtonText, String cancelButtonText)
  437. {
  438. AlertDialog.Builder builder = new AlertDialog.Builder (this);
  439. builder.setTitle (title)
  440. .setMessage (message)
  441. .setCancelable (true)
  442. .setOnCancelListener (new DialogInterface.OnCancelListener()
  443. {
  444. public void onCancel (DialogInterface dialog)
  445. {
  446. JuceAppActivity.this.alertDismissed (callback, 0);
  447. }
  448. })
  449. .setPositiveButton (okButtonText.isEmpty() ? "OK" : okButtonText, new DialogInterface.OnClickListener()
  450. {
  451. public void onClick (DialogInterface dialog, int id)
  452. {
  453. dialog.dismiss();
  454. JuceAppActivity.this.alertDismissed (callback, 1);
  455. }
  456. })
  457. .setNegativeButton (cancelButtonText.isEmpty() ? "Cancel" : cancelButtonText, new DialogInterface.OnClickListener()
  458. {
  459. public void onClick (DialogInterface dialog, int id)
  460. {
  461. dialog.dismiss();
  462. JuceAppActivity.this.alertDismissed (callback, 0);
  463. }
  464. });
  465. builder.create().show();
  466. }
  467. public final void showYesNoCancelBox (String title, String message, final long callback)
  468. {
  469. AlertDialog.Builder builder = new AlertDialog.Builder (this);
  470. builder.setTitle (title)
  471. .setMessage (message)
  472. .setCancelable (true)
  473. .setOnCancelListener (new DialogInterface.OnCancelListener()
  474. {
  475. public void onCancel (DialogInterface dialog)
  476. {
  477. JuceAppActivity.this.alertDismissed (callback, 0);
  478. }
  479. })
  480. .setPositiveButton ("Yes", new DialogInterface.OnClickListener()
  481. {
  482. public void onClick (DialogInterface dialog, int id)
  483. {
  484. dialog.dismiss();
  485. JuceAppActivity.this.alertDismissed (callback, 1);
  486. }
  487. })
  488. .setNegativeButton ("No", new DialogInterface.OnClickListener()
  489. {
  490. public void onClick (DialogInterface dialog, int id)
  491. {
  492. dialog.dismiss();
  493. JuceAppActivity.this.alertDismissed (callback, 2);
  494. }
  495. })
  496. .setNeutralButton ("Cancel", new DialogInterface.OnClickListener()
  497. {
  498. public void onClick (DialogInterface dialog, int id)
  499. {
  500. dialog.dismiss();
  501. JuceAppActivity.this.alertDismissed (callback, 0);
  502. }
  503. });
  504. builder.create().show();
  505. }
  506. public native void alertDismissed (long callback, int id);
  507. //==============================================================================
  508. public interface AppPausedResumedListener
  509. {
  510. void appPaused();
  511. void appResumed();
  512. }
  513. private Map<Long, AppPausedResumedListener> appPausedResumedListeners;
  514. public void addAppPausedResumedListener (AppPausedResumedListener l, long listenerHost)
  515. {
  516. appPausedResumedListeners.put (new Long (listenerHost), l);
  517. }
  518. public void removeAppPausedResumedListener (AppPausedResumedListener l, long listenerHost)
  519. {
  520. appPausedResumedListeners.remove (new Long (listenerHost));
  521. }
  522. //==============================================================================
  523. public final class ComponentPeerView extends ViewGroup
  524. implements View.OnFocusChangeListener, AppPausedResumedListener
  525. {
  526. public ComponentPeerView (Context context, boolean opaque_, long host)
  527. {
  528. super (context);
  529. this.host = host;
  530. setWillNotDraw (false);
  531. opaque = opaque_;
  532. setFocusable (true);
  533. setFocusableInTouchMode (true);
  534. setOnFocusChangeListener (this);
  535. // swap red and blue colours to match internal opengl texture format
  536. ColorMatrix colorMatrix = new ColorMatrix();
  537. float[] colorTransform = { 0, 0, 1.0f, 0, 0,
  538. 0, 1.0f, 0, 0, 0,
  539. 1.0f, 0, 0, 0, 0,
  540. 0, 0, 0, 1.0f, 0 };
  541. colorMatrix.set (colorTransform);
  542. paint.setColorFilter (new ColorMatrixColorFilter (colorMatrix));
  543. java.lang.reflect.Method method = null;
  544. try
  545. {
  546. method = getClass().getMethod ("setLayerType", int.class, Paint.class);
  547. }
  548. catch (SecurityException e) {}
  549. catch (NoSuchMethodException e) {}
  550. if (method != null)
  551. {
  552. try
  553. {
  554. int layerTypeNone = 0;
  555. method.invoke (this, layerTypeNone, null);
  556. }
  557. catch (java.lang.IllegalArgumentException e) {}
  558. catch (java.lang.IllegalAccessException e) {}
  559. catch (java.lang.reflect.InvocationTargetException e) {}
  560. }
  561. }
  562. //==============================================================================
  563. private native void handlePaint (long host, Canvas canvas, Paint paint);
  564. @Override
  565. public void onDraw (Canvas canvas)
  566. {
  567. if (host == 0)
  568. return;
  569. handlePaint (host, canvas, paint);
  570. }
  571. @Override
  572. public boolean isOpaque()
  573. {
  574. return opaque;
  575. }
  576. private boolean opaque;
  577. private long host;
  578. private Paint paint = new Paint();
  579. //==============================================================================
  580. private native void handleMouseDown (long host, int index, float x, float y, long time);
  581. private native void handleMouseDrag (long host, int index, float x, float y, long time);
  582. private native void handleMouseUp (long host, int index, float x, float y, long time);
  583. @Override
  584. public boolean onTouchEvent (MotionEvent event)
  585. {
  586. if (host == 0)
  587. return false;
  588. int action = event.getAction();
  589. long time = event.getEventTime();
  590. switch (action & MotionEvent.ACTION_MASK)
  591. {
  592. case MotionEvent.ACTION_DOWN:
  593. handleMouseDown (host, event.getPointerId(0), event.getX(), event.getY(), time);
  594. return true;
  595. case MotionEvent.ACTION_CANCEL:
  596. case MotionEvent.ACTION_UP:
  597. handleMouseUp (host, event.getPointerId(0), event.getX(), event.getY(), time);
  598. return true;
  599. case MotionEvent.ACTION_MOVE:
  600. {
  601. int n = event.getPointerCount();
  602. for (int i = 0; i < n; ++i)
  603. handleMouseDrag (host, event.getPointerId(i), event.getX(i), event.getY(i), time);
  604. return true;
  605. }
  606. case MotionEvent.ACTION_POINTER_UP:
  607. {
  608. int i = (action & MotionEvent.ACTION_POINTER_INDEX_MASK) >> MotionEvent.ACTION_POINTER_INDEX_SHIFT;
  609. handleMouseUp (host, event.getPointerId(i), event.getX(i), event.getY(i), time);
  610. return true;
  611. }
  612. case MotionEvent.ACTION_POINTER_DOWN:
  613. {
  614. int i = (action & MotionEvent.ACTION_POINTER_INDEX_MASK) >> MotionEvent.ACTION_POINTER_INDEX_SHIFT;
  615. handleMouseDown (host, event.getPointerId(i), event.getX(i), event.getY(i), time);
  616. return true;
  617. }
  618. default:
  619. break;
  620. }
  621. return false;
  622. }
  623. //==============================================================================
  624. private native void handleKeyDown (long host, int keycode, int textchar);
  625. private native void handleKeyUp (long host, int keycode, int textchar);
  626. private native void handleBackButton (long host);
  627. private native void handleKeyboardHidden (long host);
  628. public void showKeyboard (String type)
  629. {
  630. InputMethodManager imm = (InputMethodManager) getSystemService (Context.INPUT_METHOD_SERVICE);
  631. if (imm != null)
  632. {
  633. if (type.length() > 0)
  634. {
  635. imm.showSoftInput (this, android.view.inputmethod.InputMethodManager.SHOW_IMPLICIT);
  636. imm.setInputMethod (getWindowToken(), type);
  637. keyboardDismissListener.startListening();
  638. }
  639. else
  640. {
  641. imm.hideSoftInputFromWindow (getWindowToken(), 0);
  642. keyboardDismissListener.stopListening();
  643. }
  644. }
  645. }
  646. public void backButtonPressed()
  647. {
  648. if (host == 0)
  649. return;
  650. handleBackButton (host);
  651. }
  652. @Override
  653. public boolean onKeyDown (int keyCode, KeyEvent event)
  654. {
  655. if (host == 0)
  656. return false;
  657. switch (keyCode)
  658. {
  659. case KeyEvent.KEYCODE_VOLUME_UP:
  660. case KeyEvent.KEYCODE_VOLUME_DOWN:
  661. return super.onKeyDown (keyCode, event);
  662. case KeyEvent.KEYCODE_BACK:
  663. {
  664. ((Activity) getContext()).onBackPressed();
  665. return true;
  666. }
  667. default:
  668. break;
  669. }
  670. handleKeyDown (host, keyCode, event.getUnicodeChar());
  671. return true;
  672. }
  673. @Override
  674. public boolean onKeyUp (int keyCode, KeyEvent event)
  675. {
  676. if (host == 0)
  677. return false;
  678. handleKeyUp (host, keyCode, event.getUnicodeChar());
  679. return true;
  680. }
  681. @Override
  682. public boolean onKeyMultiple (int keyCode, int count, KeyEvent event)
  683. {
  684. if (host == 0)
  685. return false;
  686. if (keyCode != KeyEvent.KEYCODE_UNKNOWN || event.getAction() != KeyEvent.ACTION_MULTIPLE)
  687. return super.onKeyMultiple (keyCode, count, event);
  688. if (event.getCharacters() != null)
  689. {
  690. int utf8Char = event.getCharacters().codePointAt (0);
  691. handleKeyDown (host, utf8Char, utf8Char);
  692. return true;
  693. }
  694. return false;
  695. }
  696. //==============================================================================
  697. private final class KeyboardDismissListener
  698. {
  699. public KeyboardDismissListener (ComponentPeerView viewToUse)
  700. {
  701. view = viewToUse;
  702. }
  703. private void startListening()
  704. {
  705. view.getViewTreeObserver().addOnGlobalLayoutListener(viewTreeObserver);
  706. }
  707. private void stopListening()
  708. {
  709. view.getViewTreeObserver().removeGlobalOnLayoutListener(viewTreeObserver);
  710. }
  711. private class TreeObserver implements ViewTreeObserver.OnGlobalLayoutListener
  712. {
  713. TreeObserver()
  714. {
  715. keyboardShown = false;
  716. }
  717. @Override
  718. public void onGlobalLayout()
  719. {
  720. Rect r = new Rect();
  721. ViewGroup parentView = (ViewGroup) getParent();
  722. if (parentView == null)
  723. return;
  724. parentView.getWindowVisibleDisplayFrame (r);
  725. int diff = parentView.getHeight() - (r.bottom - r.top);
  726. // Arbitrary threshold, surely keyboard would take more than 20 pix.
  727. if (diff < 20 && keyboardShown)
  728. {
  729. keyboardShown = false;
  730. handleKeyboardHidden (view.host);
  731. }
  732. if (! keyboardShown && diff > 20)
  733. keyboardShown = true;
  734. };
  735. private boolean keyboardShown;
  736. };
  737. private ComponentPeerView view;
  738. private TreeObserver viewTreeObserver = new TreeObserver();
  739. }
  740. private KeyboardDismissListener keyboardDismissListener = new KeyboardDismissListener(this);
  741. // this is here to make keyboard entry work on a Galaxy Tab2 10.1
  742. @Override
  743. public InputConnection onCreateInputConnection (EditorInfo outAttrs)
  744. {
  745. outAttrs.actionLabel = "";
  746. outAttrs.hintText = "";
  747. outAttrs.initialCapsMode = 0;
  748. outAttrs.initialSelEnd = outAttrs.initialSelStart = -1;
  749. outAttrs.label = "";
  750. outAttrs.imeOptions = EditorInfo.IME_ACTION_DONE | EditorInfo.IME_FLAG_NO_EXTRACT_UI;
  751. outAttrs.inputType = InputType.TYPE_NULL;
  752. return new BaseInputConnection (this, false);
  753. }
  754. //==============================================================================
  755. @Override
  756. protected void onSizeChanged (int w, int h, int oldw, int oldh)
  757. {
  758. if (host == 0)
  759. return;
  760. super.onSizeChanged (w, h, oldw, oldh);
  761. viewSizeChanged (host);
  762. }
  763. @Override
  764. protected void onLayout (boolean changed, int left, int top, int right, int bottom)
  765. {
  766. for (int i = getChildCount(); --i >= 0;)
  767. requestTransparentRegion (getChildAt (i));
  768. }
  769. private native void viewSizeChanged (long host);
  770. @Override
  771. public void onFocusChange (View v, boolean hasFocus)
  772. {
  773. if (host == 0)
  774. return;
  775. if (v == this)
  776. focusChanged (host, hasFocus);
  777. }
  778. private native void focusChanged (long host, boolean hasFocus);
  779. public void setViewName (String newName) {}
  780. public void setSystemUiVisibilityCompat (int visibility)
  781. {
  782. Method systemUIVisibilityMethod = null;
  783. try
  784. {
  785. systemUIVisibilityMethod = this.getClass().getMethod ("setSystemUiVisibility", int.class);
  786. }
  787. catch (SecurityException e) { return; }
  788. catch (NoSuchMethodException e) { return; }
  789. if (systemUIVisibilityMethod == null) return;
  790. try
  791. {
  792. systemUIVisibilityMethod.invoke (this, visibility);
  793. }
  794. catch (java.lang.IllegalArgumentException e) {}
  795. catch (java.lang.IllegalAccessException e) {}
  796. catch (java.lang.reflect.InvocationTargetException e) {}
  797. }
  798. public boolean isVisible() { return getVisibility() == VISIBLE; }
  799. public void setVisible (boolean b) { setVisibility (b ? VISIBLE : INVISIBLE); }
  800. public boolean containsPoint (int x, int y)
  801. {
  802. return true; //xxx needs to check overlapping views
  803. }
  804. //==============================================================================
  805. private native void handleAppPaused (long host);
  806. private native void handleAppResumed (long host);
  807. @Override
  808. public void appPaused()
  809. {
  810. if (host == 0)
  811. return;
  812. handleAppPaused (host);
  813. }
  814. @Override
  815. public void appResumed()
  816. {
  817. if (host == 0)
  818. return;
  819. // Ensure that navigation/status bar visibility is correctly restored.
  820. handleAppResumed (host);
  821. }
  822. }
  823. //==============================================================================
  824. public static class NativeSurfaceView extends SurfaceView
  825. implements SurfaceHolder.Callback
  826. {
  827. private long nativeContext = 0;
  828. private boolean forVideo;
  829. NativeSurfaceView (Context context, long nativeContextPtr, boolean createdForVideo)
  830. {
  831. super (context);
  832. nativeContext = nativeContextPtr;
  833. forVideo = createdForVideo;
  834. }
  835. public Surface getNativeSurface()
  836. {
  837. Surface retval = null;
  838. SurfaceHolder holder = getHolder();
  839. if (holder != null)
  840. retval = holder.getSurface();
  841. return retval;
  842. }
  843. //==============================================================================
  844. @Override
  845. public void surfaceChanged (SurfaceHolder holder, int format, int width, int height)
  846. {
  847. if (forVideo)
  848. surfaceChangedNativeVideo (nativeContext, holder, format, width, height);
  849. else
  850. surfaceChangedNative (nativeContext, holder, format, width, height);
  851. }
  852. @Override
  853. public void surfaceCreated (SurfaceHolder holder)
  854. {
  855. if (forVideo)
  856. surfaceCreatedNativeVideo (nativeContext, holder);
  857. else
  858. surfaceCreatedNative (nativeContext, holder);
  859. }
  860. @Override
  861. public void surfaceDestroyed (SurfaceHolder holder)
  862. {
  863. if (forVideo)
  864. surfaceDestroyedNativeVideo (nativeContext, holder);
  865. else
  866. surfaceDestroyedNative (nativeContext, holder);
  867. }
  868. @Override
  869. protected void dispatchDraw (Canvas canvas)
  870. {
  871. super.dispatchDraw (canvas);
  872. if (forVideo)
  873. dispatchDrawNativeVideo (nativeContext, canvas);
  874. else
  875. dispatchDrawNative (nativeContext, canvas);
  876. }
  877. //==============================================================================
  878. @Override
  879. protected void onAttachedToWindow()
  880. {
  881. super.onAttachedToWindow();
  882. getHolder().addCallback (this);
  883. }
  884. @Override
  885. protected void onDetachedFromWindow()
  886. {
  887. super.onDetachedFromWindow();
  888. getHolder().removeCallback (this);
  889. }
  890. //==============================================================================
  891. private native void dispatchDrawNative (long nativeContextPtr, Canvas canvas);
  892. private native void surfaceCreatedNative (long nativeContextptr, SurfaceHolder holder);
  893. private native void surfaceDestroyedNative (long nativeContextptr, SurfaceHolder holder);
  894. private native void surfaceChangedNative (long nativeContextptr, SurfaceHolder holder,
  895. int format, int width, int height);
  896. private native void dispatchDrawNativeVideo (long nativeContextPtr, Canvas canvas);
  897. private native void surfaceCreatedNativeVideo (long nativeContextptr, SurfaceHolder holder);
  898. private native void surfaceDestroyedNativeVideo (long nativeContextptr, SurfaceHolder holder);
  899. private native void surfaceChangedNativeVideo (long nativeContextptr, SurfaceHolder holder,
  900. int format, int width, int height);
  901. }
  902. public NativeSurfaceView createNativeSurfaceView (long nativeSurfacePtr, boolean forVideo)
  903. {
  904. return new NativeSurfaceView (this, nativeSurfacePtr, forVideo);
  905. }
  906. //==============================================================================
  907. public final int[] renderGlyph (char glyph1, char glyph2, Paint paint, android.graphics.Matrix matrix, Rect bounds)
  908. {
  909. Path p = new Path();
  910. char[] str = { glyph1, glyph2 };
  911. paint.getTextPath (str, 0, (glyph2 != 0 ? 2 : 1), 0.0f, 0.0f, p);
  912. RectF boundsF = new RectF();
  913. p.computeBounds (boundsF, true);
  914. matrix.mapRect (boundsF);
  915. boundsF.roundOut (bounds);
  916. bounds.left--;
  917. bounds.right++;
  918. final int w = bounds.width();
  919. final int h = Math.max (1, bounds.height());
  920. Bitmap bm = Bitmap.createBitmap (w, h, Bitmap.Config.ARGB_8888);
  921. Canvas c = new Canvas (bm);
  922. matrix.postTranslate (-bounds.left, -bounds.top);
  923. c.setMatrix (matrix);
  924. c.drawPath (p, paint);
  925. final int sizeNeeded = w * h;
  926. if (cachedRenderArray.length < sizeNeeded)
  927. cachedRenderArray = new int [sizeNeeded];
  928. bm.getPixels (cachedRenderArray, 0, w, 0, 0, w, h);
  929. bm.recycle();
  930. return cachedRenderArray;
  931. }
  932. private int[] cachedRenderArray = new int [256];
  933. //==============================================================================
  934. public static class NativeInvocationHandler implements InvocationHandler
  935. {
  936. public NativeInvocationHandler (Activity activityToUse, long nativeContextRef)
  937. {
  938. activity = activityToUse;
  939. nativeContext = nativeContextRef;
  940. }
  941. public void nativeContextDeleted()
  942. {
  943. nativeContext = 0;
  944. }
  945. @Override
  946. public void finalize()
  947. {
  948. activity.runOnUiThread (new Runnable()
  949. {
  950. @Override
  951. public void run()
  952. {
  953. if (nativeContext != 0)
  954. dispatchFinalize (nativeContext);
  955. }
  956. });
  957. }
  958. @Override
  959. public Object invoke (Object proxy, Method method, Object[] args) throws Throwable
  960. {
  961. return dispatchInvoke (nativeContext, proxy, method, args);
  962. }
  963. //==============================================================================
  964. Activity activity;
  965. private long nativeContext = 0;
  966. private native void dispatchFinalize (long nativeContextRef);
  967. private native Object dispatchInvoke (long nativeContextRef, Object proxy, Method method, Object[] args);
  968. }
  969. public InvocationHandler createInvocationHandler (long nativeContextRef)
  970. {
  971. return new NativeInvocationHandler (this, nativeContextRef);
  972. }
  973. public void invocationHandlerContextDeleted (InvocationHandler handler)
  974. {
  975. ((NativeInvocationHandler) handler).nativeContextDeleted();
  976. }
  977. //==============================================================================
  978. public static class HTTPStream
  979. {
  980. public HTTPStream (String address, boolean isPostToUse, byte[] postDataToUse,
  981. String headersToUse, int timeOutMsToUse,
  982. int[] statusCodeToUse, StringBuffer responseHeadersToUse,
  983. int numRedirectsToFollowToUse, String httpRequestCmdToUse) throws IOException
  984. {
  985. isPost = isPostToUse;
  986. postData = postDataToUse;
  987. headers = headersToUse;
  988. timeOutMs = timeOutMsToUse;
  989. statusCode = statusCodeToUse;
  990. responseHeaders = responseHeadersToUse;
  991. totalLength = -1;
  992. numRedirectsToFollow = numRedirectsToFollowToUse;
  993. httpRequestCmd = httpRequestCmdToUse;
  994. connection = createConnection (address, isPost, postData, headers, timeOutMs, httpRequestCmd);
  995. }
  996. private final HttpURLConnection createConnection (String address, boolean isPost, byte[] postData,
  997. String headers, int timeOutMs, String httpRequestCmdToUse) throws IOException
  998. {
  999. HttpURLConnection newConnection = (HttpURLConnection) (new URL(address).openConnection());
  1000. try
  1001. {
  1002. newConnection.setInstanceFollowRedirects (false);
  1003. newConnection.setConnectTimeout (timeOutMs);
  1004. newConnection.setReadTimeout (timeOutMs);
  1005. // 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.
  1006. // So convert headers string to an array, with an element for each line
  1007. String headerLines[] = headers.split("\\n");
  1008. // Set request headers
  1009. for (int i = 0; i < headerLines.length; ++i)
  1010. {
  1011. int pos = headerLines[i].indexOf (":");
  1012. if (pos > 0 && pos < headerLines[i].length())
  1013. {
  1014. String field = headerLines[i].substring (0, pos);
  1015. String value = headerLines[i].substring (pos + 1);
  1016. if (value.length() > 0)
  1017. newConnection.setRequestProperty (field, value);
  1018. }
  1019. }
  1020. newConnection.setRequestMethod (httpRequestCmd);
  1021. if (isPost)
  1022. {
  1023. newConnection.setDoOutput (true);
  1024. if (postData != null)
  1025. {
  1026. OutputStream out = newConnection.getOutputStream();
  1027. out.write(postData);
  1028. out.flush();
  1029. }
  1030. }
  1031. return newConnection;
  1032. }
  1033. catch (Throwable e)
  1034. {
  1035. newConnection.disconnect();
  1036. throw new IOException ("Connection error");
  1037. }
  1038. }
  1039. private final InputStream getCancellableStream (final boolean isInput) throws ExecutionException
  1040. {
  1041. synchronized (createFutureLock)
  1042. {
  1043. if (hasBeenCancelled.get())
  1044. return null;
  1045. streamFuture = executor.submit (new Callable<BufferedInputStream>()
  1046. {
  1047. @Override
  1048. public BufferedInputStream call() throws IOException
  1049. {
  1050. return new BufferedInputStream (isInput ? connection.getInputStream()
  1051. : connection.getErrorStream());
  1052. }
  1053. });
  1054. }
  1055. try
  1056. {
  1057. return streamFuture.get();
  1058. }
  1059. catch (InterruptedException e)
  1060. {
  1061. return null;
  1062. }
  1063. catch (CancellationException e)
  1064. {
  1065. return null;
  1066. }
  1067. }
  1068. public final boolean connect()
  1069. {
  1070. boolean result = false;
  1071. int numFollowedRedirects = 0;
  1072. while (true)
  1073. {
  1074. result = doConnect();
  1075. if (! result)
  1076. return false;
  1077. if (++numFollowedRedirects > numRedirectsToFollow)
  1078. break;
  1079. int status = statusCode[0];
  1080. if (status == 301 || status == 302 || status == 303 || status == 307)
  1081. {
  1082. // Assumes only one occurrence of "Location"
  1083. int pos1 = responseHeaders.indexOf ("Location:") + 10;
  1084. int pos2 = responseHeaders.indexOf ("\n", pos1);
  1085. if (pos2 > pos1)
  1086. {
  1087. String currentLocation = connection.getURL().toString();
  1088. String newLocation = responseHeaders.substring (pos1, pos2);
  1089. try
  1090. {
  1091. // Handle newLocation whether it's absolute or relative
  1092. URL baseUrl = new URL (currentLocation);
  1093. URL newUrl = new URL (baseUrl, newLocation);
  1094. String transformedNewLocation = newUrl.toString();
  1095. if (transformedNewLocation != currentLocation)
  1096. {
  1097. // Clear responseHeaders before next iteration
  1098. responseHeaders.delete (0, responseHeaders.length());
  1099. synchronized (createStreamLock)
  1100. {
  1101. if (hasBeenCancelled.get())
  1102. return false;
  1103. connection.disconnect();
  1104. try
  1105. {
  1106. connection = createConnection (transformedNewLocation, isPost,
  1107. postData, headers, timeOutMs,
  1108. httpRequestCmd);
  1109. }
  1110. catch (Throwable e)
  1111. {
  1112. return false;
  1113. }
  1114. }
  1115. }
  1116. else
  1117. {
  1118. break;
  1119. }
  1120. }
  1121. catch (Throwable e)
  1122. {
  1123. return false;
  1124. }
  1125. }
  1126. else
  1127. {
  1128. break;
  1129. }
  1130. }
  1131. else
  1132. {
  1133. break;
  1134. }
  1135. }
  1136. return result;
  1137. }
  1138. private final boolean doConnect()
  1139. {
  1140. synchronized (createStreamLock)
  1141. {
  1142. if (hasBeenCancelled.get())
  1143. return false;
  1144. try
  1145. {
  1146. try
  1147. {
  1148. inputStream = getCancellableStream (true);
  1149. }
  1150. catch (ExecutionException e)
  1151. {
  1152. if (connection.getResponseCode() < 400)
  1153. {
  1154. statusCode[0] = connection.getResponseCode();
  1155. connection.disconnect();
  1156. return false;
  1157. }
  1158. }
  1159. finally
  1160. {
  1161. statusCode[0] = connection.getResponseCode();
  1162. }
  1163. try
  1164. {
  1165. if (statusCode[0] >= 400)
  1166. inputStream = getCancellableStream (false);
  1167. else
  1168. inputStream = getCancellableStream (true);
  1169. }
  1170. catch (ExecutionException e)
  1171. {}
  1172. for (java.util.Map.Entry<String, java.util.List<String>> entry : connection.getHeaderFields().entrySet())
  1173. {
  1174. if (entry.getKey() != null && entry.getValue() != null)
  1175. {
  1176. responseHeaders.append(entry.getKey() + ": "
  1177. + android.text.TextUtils.join(",", entry.getValue()) + "\n");
  1178. if (entry.getKey().compareTo ("Content-Length") == 0)
  1179. totalLength = Integer.decode (entry.getValue().get (0));
  1180. }
  1181. }
  1182. return true;
  1183. }
  1184. catch (IOException e)
  1185. {
  1186. return false;
  1187. }
  1188. }
  1189. }
  1190. static class DisconnectionRunnable implements Runnable
  1191. {
  1192. public DisconnectionRunnable (HttpURLConnection theConnection,
  1193. InputStream theInputStream,
  1194. ReentrantLock theCreateStreamLock,
  1195. Object theCreateFutureLock,
  1196. Future<BufferedInputStream> theStreamFuture)
  1197. {
  1198. connectionToDisconnect = theConnection;
  1199. inputStream = theInputStream;
  1200. createStreamLock = theCreateStreamLock;
  1201. createFutureLock = theCreateFutureLock;
  1202. streamFuture = theStreamFuture;
  1203. }
  1204. public void run()
  1205. {
  1206. try
  1207. {
  1208. if (! createStreamLock.tryLock())
  1209. {
  1210. synchronized (createFutureLock)
  1211. {
  1212. if (streamFuture != null)
  1213. streamFuture.cancel (true);
  1214. }
  1215. createStreamLock.lock();
  1216. }
  1217. if (connectionToDisconnect != null)
  1218. connectionToDisconnect.disconnect();
  1219. if (inputStream != null)
  1220. inputStream.close();
  1221. }
  1222. catch (IOException e)
  1223. {}
  1224. finally
  1225. {
  1226. createStreamLock.unlock();
  1227. }
  1228. }
  1229. private HttpURLConnection connectionToDisconnect;
  1230. private InputStream inputStream;
  1231. private ReentrantLock createStreamLock;
  1232. private Object createFutureLock;
  1233. Future<BufferedInputStream> streamFuture;
  1234. }
  1235. public final void release()
  1236. {
  1237. DisconnectionRunnable disconnectionRunnable = new DisconnectionRunnable (connection,
  1238. inputStream,
  1239. createStreamLock,
  1240. createFutureLock,
  1241. streamFuture);
  1242. synchronized (createStreamLock)
  1243. {
  1244. hasBeenCancelled.set (true);
  1245. connection = null;
  1246. }
  1247. Thread disconnectionThread = new Thread(disconnectionRunnable);
  1248. disconnectionThread.start();
  1249. }
  1250. public final int read (byte[] buffer, int numBytes)
  1251. {
  1252. int num = 0;
  1253. try
  1254. {
  1255. synchronized (createStreamLock)
  1256. {
  1257. if (inputStream != null)
  1258. num = inputStream.read (buffer, 0, numBytes);
  1259. }
  1260. }
  1261. catch (IOException e)
  1262. {}
  1263. if (num > 0)
  1264. position += num;
  1265. return num;
  1266. }
  1267. public final long getPosition() { return position; }
  1268. public final long getTotalLength() { return totalLength; }
  1269. public final boolean isExhausted() { return false; }
  1270. public final boolean setPosition (long newPos) { return false; }
  1271. private boolean isPost;
  1272. private byte[] postData;
  1273. private String headers;
  1274. private int timeOutMs;
  1275. String httpRequestCmd;
  1276. private HttpURLConnection connection;
  1277. private int[] statusCode;
  1278. private StringBuffer responseHeaders;
  1279. private int totalLength;
  1280. private int numRedirectsToFollow;
  1281. private InputStream inputStream;
  1282. private long position;
  1283. private final ReentrantLock createStreamLock = new ReentrantLock();
  1284. private final Object createFutureLock = new Object();
  1285. private AtomicBoolean hasBeenCancelled = new AtomicBoolean();
  1286. private final ExecutorService executor = Executors.newCachedThreadPool (Executors.defaultThreadFactory());
  1287. Future<BufferedInputStream> streamFuture;
  1288. }
  1289. public static final HTTPStream createHTTPStream (String address, boolean isPost, byte[] postData,
  1290. String headers, int timeOutMs, int[] statusCode,
  1291. StringBuffer responseHeaders, int numRedirectsToFollow,
  1292. String httpRequestCmd)
  1293. {
  1294. // timeout parameter of zero for HttpUrlConnection is a blocking connect (negative value for juce::URL)
  1295. if (timeOutMs < 0)
  1296. timeOutMs = 0;
  1297. else if (timeOutMs == 0)
  1298. timeOutMs = 30000;
  1299. for (;;)
  1300. {
  1301. try
  1302. {
  1303. HTTPStream httpStream = new HTTPStream (address, isPost, postData, headers,
  1304. timeOutMs, statusCode, responseHeaders,
  1305. numRedirectsToFollow, httpRequestCmd);
  1306. return httpStream;
  1307. }
  1308. catch (Throwable e) {}
  1309. return null;
  1310. }
  1311. }
  1312. public final void launchURL (String url)
  1313. {
  1314. startActivity (new Intent (Intent.ACTION_VIEW, Uri.parse (url)));
  1315. }
  1316. private native boolean webViewPageLoadStarted (long host, WebView view, String url);
  1317. private native void webViewPageLoadFinished (long host, WebView view, String url);
  1318. $$JuceAndroidWebViewNativeCode$$ // If you get an error here, you need to re-save your project with the Projucer!
  1319. private native void webViewReceivedSslError (long host, WebView view, SslErrorHandler handler, SslError error);
  1320. private native void webViewCloseWindowRequest (long host, WebView view);
  1321. private native void webViewCreateWindowRequest (long host, WebView view);
  1322. //==============================================================================
  1323. public class JuceWebViewClient extends WebViewClient
  1324. {
  1325. public JuceWebViewClient (long hostToUse)
  1326. {
  1327. host = hostToUse;
  1328. }
  1329. public void hostDeleted()
  1330. {
  1331. synchronized (hostLock)
  1332. {
  1333. host = 0;
  1334. }
  1335. }
  1336. @Override
  1337. public void onPageFinished (WebView view, String url)
  1338. {
  1339. if (host == 0)
  1340. return;
  1341. webViewPageLoadFinished (host, view, url);
  1342. }
  1343. @Override
  1344. public void onReceivedSslError (WebView view, SslErrorHandler handler, SslError error)
  1345. {
  1346. if (host == 0)
  1347. return;
  1348. webViewReceivedSslError (host, view, handler, error);
  1349. }
  1350. $$JuceAndroidWebViewCode$$ // If you get an error here, you need to re-save your project with the Projucer!
  1351. private long host;
  1352. private final Object hostLock = new Object();
  1353. }
  1354. public class JuceWebChromeClient extends WebChromeClient
  1355. {
  1356. public JuceWebChromeClient (long hostToUse)
  1357. {
  1358. host = hostToUse;
  1359. }
  1360. @Override
  1361. public void onCloseWindow (WebView window)
  1362. {
  1363. webViewCloseWindowRequest (host, window);
  1364. }
  1365. @Override
  1366. public boolean onCreateWindow (WebView view, boolean isDialog,
  1367. boolean isUserGesture, Message resultMsg)
  1368. {
  1369. webViewCreateWindowRequest (host, view);
  1370. return false;
  1371. }
  1372. private long host;
  1373. private final Object hostLock = new Object();
  1374. }
  1375. $$JuceAndroidCameraCode$$ // If you get an error here, you need to re-save your project with the Projucer!
  1376. $$JuceAndroidVideoCode$$ // If you get an error here, you need to re-save your project with the Projucer!
  1377. //==============================================================================
  1378. public static final String getLocaleValue (boolean isRegion)
  1379. {
  1380. java.util.Locale locale = java.util.Locale.getDefault();
  1381. return isRegion ? locale.getCountry()
  1382. : locale.getLanguage();
  1383. }
  1384. private static final String getFileLocation (String type)
  1385. {
  1386. return Environment.getExternalStoragePublicDirectory (type).getAbsolutePath();
  1387. }
  1388. public static final String getDocumentsFolder()
  1389. {
  1390. if (getAndroidSDKVersion() >= 19)
  1391. return getFileLocation ("Documents");
  1392. return Environment.getDataDirectory().getAbsolutePath();
  1393. }
  1394. public static final String getPicturesFolder() { return getFileLocation (Environment.DIRECTORY_PICTURES); }
  1395. public static final String getMusicFolder() { return getFileLocation (Environment.DIRECTORY_MUSIC); }
  1396. public static final String getMoviesFolder() { return getFileLocation (Environment.DIRECTORY_MOVIES); }
  1397. public static final String getDownloadsFolder() { return getFileLocation (Environment.DIRECTORY_DOWNLOADS); }
  1398. //==============================================================================
  1399. @Override
  1400. protected void onActivityResult (int requestCode, int resultCode, Intent data)
  1401. {
  1402. appActivityResult (requestCode, resultCode, data);
  1403. }
  1404. @Override
  1405. protected void onNewIntent (Intent intent)
  1406. {
  1407. super.onNewIntent(intent);
  1408. setIntent(intent);
  1409. appNewIntent (intent);
  1410. }
  1411. //==============================================================================
  1412. public final Typeface getTypeFaceFromAsset (String assetName)
  1413. {
  1414. try
  1415. {
  1416. return Typeface.createFromAsset (this.getResources().getAssets(), assetName);
  1417. }
  1418. catch (Throwable e) {}
  1419. return null;
  1420. }
  1421. final protected static char[] hexArray = "0123456789ABCDEF".toCharArray();
  1422. public static String bytesToHex (byte[] bytes)
  1423. {
  1424. char[] hexChars = new char[bytes.length * 2];
  1425. for (int j = 0; j < bytes.length; ++j)
  1426. {
  1427. int v = bytes[j] & 0xff;
  1428. hexChars[j * 2] = hexArray[v >>> 4];
  1429. hexChars[j * 2 + 1] = hexArray[v & 0x0f];
  1430. }
  1431. return new String (hexChars);
  1432. }
  1433. final private java.util.Map dataCache = new java.util.HashMap();
  1434. synchronized private final File getDataCacheFile (byte[] data)
  1435. {
  1436. try
  1437. {
  1438. java.security.MessageDigest digest = java.security.MessageDigest.getInstance ("MD5");
  1439. digest.update (data);
  1440. String key = bytesToHex (digest.digest());
  1441. if (dataCache.containsKey (key))
  1442. return (File) dataCache.get (key);
  1443. File f = new File (this.getCacheDir(), "bindata_" + key);
  1444. f.delete();
  1445. FileOutputStream os = new FileOutputStream (f);
  1446. os.write (data, 0, data.length);
  1447. dataCache.put (key, f);
  1448. return f;
  1449. }
  1450. catch (Throwable e) {}
  1451. return null;
  1452. }
  1453. private final void clearDataCache()
  1454. {
  1455. java.util.Iterator it = dataCache.values().iterator();
  1456. while (it.hasNext())
  1457. {
  1458. File f = (File) it.next();
  1459. f.delete();
  1460. }
  1461. }
  1462. public final Typeface getTypeFaceFromByteArray (byte[] data)
  1463. {
  1464. try
  1465. {
  1466. File f = getDataCacheFile (data);
  1467. if (f != null)
  1468. return Typeface.createFromFile (f);
  1469. }
  1470. catch (Exception e)
  1471. {
  1472. Log.e ("JUCE", e.toString());
  1473. }
  1474. return null;
  1475. }
  1476. public static final int getAndroidSDKVersion()
  1477. {
  1478. return android.os.Build.VERSION.SDK_INT;
  1479. }
  1480. public final String audioManagerGetProperty (String property)
  1481. {
  1482. Object obj = getSystemService (AUDIO_SERVICE);
  1483. if (obj == null)
  1484. return null;
  1485. java.lang.reflect.Method method;
  1486. try
  1487. {
  1488. method = obj.getClass().getMethod ("getProperty", String.class);
  1489. }
  1490. catch (SecurityException e) { return null; }
  1491. catch (NoSuchMethodException e) { return null; }
  1492. if (method == null)
  1493. return null;
  1494. try
  1495. {
  1496. return (String) method.invoke (obj, property);
  1497. }
  1498. catch (java.lang.IllegalArgumentException e) {}
  1499. catch (java.lang.IllegalAccessException e) {}
  1500. catch (java.lang.reflect.InvocationTargetException e) {}
  1501. return null;
  1502. }
  1503. public final boolean hasSystemFeature (String property)
  1504. {
  1505. return getPackageManager().hasSystemFeature (property);
  1506. }
  1507. }