3 Copyright (C) 2014 celeron55, Perttu Ahola <celeron55@gmail.com>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 #error This file may only be compiled for android!
24 #include "util/numeric.h"
26 #include "porting_android.h"
27 #include "threading/thread.h"
40 extern int main(int argc, char *argv[]);
42 void android_main(android_app *app)
45 porting::app_global = app;
47 Thread::setName("Main");
50 char *argv[] = {strdup(PROJECT_NAME), nullptr};
51 main(ARRLEN(argv) - 1, argv);
53 } catch (std::exception &e) {
54 errorstream << "Uncaught exception in main thread: " << e.what() << std::endl;
57 errorstream << "Uncaught exception in main thread!" << std::endl;
61 porting::cleanupAndroid();
62 infostream << "Shutting down." << std::endl;
67 * Handler for finished message box input
68 * Intentionally NOT in namespace porting
69 * ToDo: this doesn't work as expected, there's a workaround for it right now
72 JNIEXPORT void JNICALL Java_net_minetest_minetest_GameActivity_putMessageBoxResult(
73 JNIEnv *env, jclass thiz, jstring text)
76 "Java_net_minetest_minetest_GameActivity_putMessageBoxResult got: " <<
77 std::string((const char*) env->GetStringChars(text, nullptr)) << std::endl;
82 android_app *app_global;
84 jclass nativeActivity;
86 jclass findClass(const std::string &classname)
88 if (jnienv == nullptr)
91 jclass nativeactivity = jnienv->FindClass("android/app/NativeActivity");
92 jmethodID getClassLoader = jnienv->GetMethodID(
93 nativeactivity, "getClassLoader", "()Ljava/lang/ClassLoader;");
94 jobject cls = jnienv->CallObjectMethod(
95 app_global->activity->clazz, getClassLoader);
96 jclass classLoader = jnienv->FindClass("java/lang/ClassLoader");
97 jmethodID findClass = jnienv->GetMethodID(classLoader, "loadClass",
98 "(Ljava/lang/String;)Ljava/lang/Class;");
99 jstring strClassName = jnienv->NewStringUTF(classname.c_str());
100 return (jclass) jnienv->CallObjectMethod(cls, findClass, strClassName);
105 porting::jnienv = nullptr;
106 JavaVM *jvm = app_global->activity->vm;
107 JavaVMAttachArgs lJavaVMAttachArgs;
108 lJavaVMAttachArgs.version = JNI_VERSION_1_6;
109 lJavaVMAttachArgs.name = PROJECT_NAME_C "NativeThread";
110 lJavaVMAttachArgs.group = nullptr;
112 if (jvm->AttachCurrentThread(&porting::jnienv, &lJavaVMAttachArgs) == JNI_ERR) {
113 errorstream << "Failed to attach native thread to jvm" << std::endl;
117 nativeActivity = findClass("net/minetest/minetest/GameActivity");
118 if (nativeActivity == nullptr)
120 "porting::initAndroid unable to find java native activity class" <<
124 // in the start-up code
125 __android_log_print(ANDROID_LOG_ERROR, PROJECT_NAME_C,
126 "Initializing GPROF profiler");
127 monstartup("libMinetest.so");
131 void cleanupAndroid()
134 errorstream << "Shutting down GPROF profiler" << std::endl;
135 setenv("CPUPROFILE", (path_user + DIR_DELIM + "gmon.out").c_str(), 1);
139 JavaVM *jvm = app_global->activity->vm;
140 jvm->DetachCurrentThread();
143 static std::string javaStringToUTF8(jstring js)
146 // Get string as a UTF-8 c-string
147 const char *c_str = jnienv->GetStringUTFChars(js, nullptr);
150 // And free the c-string
151 jnienv->ReleaseStringUTFChars(js, c_str);
155 // Calls static method if obj is NULL
156 static std::string getAndroidPath(
157 jclass cls, jobject obj, jmethodID mt_getAbsPath, const char *getter)
162 mt_getter = jnienv->GetMethodID(cls, getter, "()Ljava/io/File;");
164 mt_getter = jnienv->GetStaticMethodID(cls, getter, "()Ljava/io/File;");
169 ob_file = jnienv->CallObjectMethod(obj, mt_getter);
171 ob_file = jnienv->CallStaticObjectMethod(cls, mt_getter);
173 // Call getAbsolutePath
174 auto js_path = (jstring) jnienv->CallObjectMethod(ob_file, mt_getAbsPath);
176 return javaStringToUTF8(js_path);
179 void initializePathsAndroid()
181 // Get Environment class
182 jclass cls_Env = jnienv->FindClass("android/os/Environment");
184 jclass cls_File = jnienv->FindClass("java/io/File");
185 // Get getAbsolutePath method
186 jmethodID mt_getAbsPath = jnienv->GetMethodID(cls_File,
187 "getAbsolutePath", "()Ljava/lang/String;");
188 std::string path_storage = getAndroidPath(cls_Env, nullptr,
189 mt_getAbsPath, "getExternalStorageDirectory");
191 path_user = path_storage + DIR_DELIM + PROJECT_NAME_C;
192 path_share = path_storage + DIR_DELIM + PROJECT_NAME_C;
193 path_cache = getAndroidPath(nativeActivity,
194 app_global->activity->clazz, mt_getAbsPath, "getCacheDir");
198 void showInputDialog(const std::string &acceptButton, const std::string &hint,
199 const std::string ¤t, int editType)
201 jmethodID showdialog = jnienv->GetMethodID(nativeActivity, "showDialog",
202 "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;I)V");
204 FATAL_ERROR_IF(showdialog == nullptr,
205 "porting::showInputDialog unable to find java show dialog method");
207 jstring jacceptButton = jnienv->NewStringUTF(acceptButton.c_str());
208 jstring jhint = jnienv->NewStringUTF(hint.c_str());
209 jstring jcurrent = jnienv->NewStringUTF(current.c_str());
210 jint jeditType = editType;
212 jnienv->CallVoidMethod(app_global->activity->clazz, showdialog,
213 jacceptButton, jhint, jcurrent, jeditType);
216 void openURLAndroid(const std::string &url)
218 jmethodID url_open = jnienv->GetMethodID(nativeActivity, "openURL",
219 "(Ljava/lang/String;)V");
221 FATAL_ERROR_IF(url_open == nullptr,
222 "porting::openURLAndroid unable to find java openURL method");
224 jstring jurl = jnienv->NewStringUTF(url.c_str());
225 jnienv->CallVoidMethod(app_global->activity->clazz, url_open, jurl);
228 int getInputDialogState()
230 jmethodID dialogstate = jnienv->GetMethodID(nativeActivity,
231 "getDialogState", "()I");
233 FATAL_ERROR_IF(dialogstate == nullptr,
234 "porting::getInputDialogState unable to find java dialog state method");
236 return jnienv->CallIntMethod(app_global->activity->clazz, dialogstate);
239 std::string getInputDialogValue()
241 jmethodID dialogvalue = jnienv->GetMethodID(nativeActivity,
242 "getDialogValue", "()Ljava/lang/String;");
244 FATAL_ERROR_IF(dialogvalue == nullptr,
245 "porting::getInputDialogValue unable to find java dialog value method");
247 jobject result = jnienv->CallObjectMethod(app_global->activity->clazz,
250 const char *javachars = jnienv->GetStringUTFChars((jstring) result, nullptr);
251 std::string text(javachars);
252 jnienv->ReleaseStringUTFChars((jstring) result, javachars);
258 float getDisplayDensity()
260 static bool firstrun = true;
261 static float value = 0;
264 jmethodID getDensity = jnienv->GetMethodID(nativeActivity,
265 "getDensity", "()F");
267 FATAL_ERROR_IF(getDensity == nullptr,
268 "porting::getDisplayDensity unable to find java getDensity method");
270 value = jnienv->CallFloatMethod(app_global->activity->clazz, getDensity);
276 v2u32 getDisplaySize()
278 static bool firstrun = true;
282 jmethodID getDisplayWidth = jnienv->GetMethodID(nativeActivity,
283 "getDisplayWidth", "()I");
285 FATAL_ERROR_IF(getDisplayWidth == nullptr,
286 "porting::getDisplayWidth unable to find java getDisplayWidth method");
288 retval.X = jnienv->CallIntMethod(app_global->activity->clazz,
291 jmethodID getDisplayHeight = jnienv->GetMethodID(nativeActivity,
292 "getDisplayHeight", "()I");
294 FATAL_ERROR_IF(getDisplayHeight == nullptr,
295 "porting::getDisplayHeight unable to find java getDisplayHeight method");
297 retval.Y = jnienv->CallIntMethod(app_global->activity->clazz,
304 #endif // ndef SERVER