AndroidApiWrapper.cc 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /*
  2. * Android API wrapper
  3. *
  4. * It makes a few Android functions available for the android flavour of openMSX
  5. *
  6. */
  7. #include "build-info.hh"
  8. #include "AndroidApiWrapper.hh"
  9. #if PLATFORM_ANDROID
  10. #include "openmsx.hh"
  11. #include <stdlib.h>
  12. #include <string.h>
  13. #include <jni.h>
  14. // The jniVM parameter gets set by the JNI mechanism directly after loading the
  15. // shared lib containing openMSX by invoking a method with following
  16. // signature if it exists in the shared lib:
  17. // JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *vm, void *reserved)
  18. // This method is defined at the end of this source file and must be able to
  19. // initialize this jniVM parameter, hence it must exist outside the openmsx
  20. // namespace
  21. static JavaVM *jniVM = NULL;
  22. namespace openmsx {
  23. std::string AndroidApiWrapper::getStorageDirectory()
  24. {
  25. JNIEnv * jniEnv = NULL;
  26. jclass cls;
  27. jmethodID mid;
  28. jobject storageDirectory;
  29. jstring storageDirectoryName;
  30. jniVM->AttachCurrentThread(&jniEnv, NULL);
  31. if( !jniEnv ) {
  32. throw JniException("Java VM AttachCurrentThread() failed");
  33. }
  34. cls = jniEnv->FindClass("android/os/Environment");
  35. if (cls == 0) {
  36. throw JniException("Cant find class android/os/Environment");
  37. }
  38. mid = jniEnv->GetStaticMethodID(cls, "getExternalStorageDirectory", "()Ljava/io/File;");
  39. if (mid == 0) {
  40. throw JniException("Cant find getExternalStorageDirectory method");
  41. }
  42. storageDirectory = jniEnv->CallStaticObjectMethod(cls, mid);
  43. if (storageDirectory == 0) {
  44. throw JniException("Cant get storageDirectory");
  45. }
  46. cls = jniEnv->GetObjectClass(storageDirectory);
  47. if (cls == 0) {
  48. throw JniException("Cant find class for storageDirectory object");
  49. }
  50. mid = jniEnv->GetMethodID(cls, "getAbsolutePath", "()Ljava/lang/String;");
  51. if (mid == 0) {
  52. throw JniException("Cant find getAbsolutePath method");
  53. }
  54. storageDirectoryName = (jstring)jniEnv->CallObjectMethod(storageDirectory, mid);
  55. if (storageDirectoryName == 0) {
  56. throw JniException("Cant get storageDirectoryName");
  57. }
  58. const char *str = jniEnv->GetStringUTFChars(storageDirectoryName, NULL);
  59. if (str == NULL) {
  60. throw JniException("Cant convert storageDirectoryName to C format");
  61. }
  62. std::string rslt(str);
  63. jniEnv->ReleaseStringUTFChars(storageDirectoryName, str);
  64. return rslt;
  65. }
  66. } // namespace openmsx
  67. JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *vm, void * /*reserved*/)
  68. {
  69. // Store the reference to the JVM so that the JNI calls can use it
  70. jniVM = vm;
  71. return JNI_VERSION_1_2;
  72. };
  73. JNIEXPORT void JNICALL JNI_OnUnload(JavaVM * /*vm*/, void * /*reserved*/)
  74. {
  75. // Nothing to do
  76. };
  77. #endif