GoNativeActivity.java 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. package org.golang.app;
  2. import android.app.Activity;
  3. import android.app.NativeActivity;
  4. import android.content.Context;
  5. import android.content.pm.ActivityInfo;
  6. import android.content.pm.PackageManager;
  7. import android.os.Bundle;
  8. import android.util.Log;
  9. import android.view.KeyCharacterMap;
  10. public class GoNativeActivity extends NativeActivity {
  11. private static GoNativeActivity goNativeActivity;
  12. public GoNativeActivity() {
  13. super();
  14. goNativeActivity = this;
  15. }
  16. String getTmpdir() {
  17. return getCacheDir().getAbsolutePath();
  18. }
  19. static int getRune(int deviceId, int keyCode, int metaState) {
  20. try {
  21. int rune = KeyCharacterMap.load(deviceId).get(keyCode, metaState);
  22. if (rune == 0) {
  23. return -1;
  24. }
  25. return rune;
  26. } catch (KeyCharacterMap.UnavailableException e) {
  27. return -1;
  28. } catch (Exception e) {
  29. Log.e("Go", "exception reading KeyCharacterMap", e);
  30. return -1;
  31. }
  32. }
  33. private void load() {
  34. // Interestingly, NativeActivity uses a different method
  35. // to find native code to execute, avoiding
  36. // System.loadLibrary. The result is Java methods
  37. // implemented in C with JNIEXPORT (and JNI_OnLoad) are not
  38. // available unless an explicit call to System.loadLibrary
  39. // is done. So we do it here, borrowing the name of the
  40. // library from the same AndroidManifest.xml metadata used
  41. // by NativeActivity.
  42. try {
  43. ActivityInfo ai = getPackageManager().getActivityInfo(
  44. getIntent().getComponent(), PackageManager.GET_META_DATA);
  45. if (ai.metaData == null) {
  46. Log.e("Go", "loadLibrary: no manifest metadata found");
  47. return;
  48. }
  49. String libName = ai.metaData.getString("android.app.lib_name");
  50. System.loadLibrary(libName);
  51. } catch (Exception e) {
  52. Log.e("Go", "loadLibrary failed", e);
  53. }
  54. }
  55. @Override
  56. public void onCreate(Bundle savedInstanceState) {
  57. load();
  58. super.onCreate(savedInstanceState);
  59. }
  60. }