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()
58 errorstream << "Uncaught exception in main thread!" << std::endl;
62 porting::cleanupAndroid();
63 infostream << "Shutting down." << std::endl;
68 * Handler for finished message box input
69 * Intentionally NOT in namespace porting
70 * ToDo: this doesn't work as expected, there's a workaround for it right now
73 JNIEXPORT void JNICALL Java_net_minetest_minetest_GameActivity_putMessageBoxResult(
74 JNIEnv *env, jclass thiz, jstring text)
76 errorstream << "Java_net_minetest_minetest_GameActivity_putMessageBoxResult got: "
77 << std::string((const char *)env->GetStringChars(text, nullptr))
84 android_app *app_global;
86 jclass nativeActivity;
88 jclass findClass(const std::string &classname)
90 if (jnienv == nullptr)
93 jclass nativeactivity = jnienv->FindClass("android/app/NativeActivity");
94 jmethodID getClassLoader = jnienv->GetMethodID(
95 nativeactivity, "getClassLoader", "()Ljava/lang/ClassLoader;");
96 jobject cls = jnienv->CallObjectMethod(
97 app_global->activity->clazz, getClassLoader);
98 jclass classLoader = jnienv->FindClass("java/lang/ClassLoader");
99 jmethodID findClass = jnienv->GetMethodID(classLoader, "loadClass",
100 "(Ljava/lang/String;)Ljava/lang/Class;");
101 jstring strClassName = jnienv->NewStringUTF(classname.c_str());
102 return (jclass)jnienv->CallObjectMethod(cls, findClass, strClassName);
107 porting::jnienv = nullptr;
108 JavaVM *jvm = app_global->activity->vm;
109 JavaVMAttachArgs lJavaVMAttachArgs;
110 lJavaVMAttachArgs.version = JNI_VERSION_1_6;
111 lJavaVMAttachArgs.name = PROJECT_NAME_C "NativeThread";
112 lJavaVMAttachArgs.group = nullptr;
114 if (jvm->AttachCurrentThread(&porting::jnienv, &lJavaVMAttachArgs) == JNI_ERR) {
115 errorstream << "Failed to attach native thread to jvm" << std::endl;
119 nativeActivity = findClass("net/minetest/minetest/GameActivity");
120 if (nativeActivity == nullptr)
121 errorstream << "porting::initAndroid unable to find java native activity "
126 // in the start-up code
128 ANDROID_LOG_ERROR, PROJECT_NAME_C, "Initializing GPROF profiler");
129 monstartup("libMinetest.so");
133 void cleanupAndroid()
136 errorstream << "Shutting down GPROF profiler" << std::endl;
137 setenv("CPUPROFILE", (path_user + DIR_DELIM + "gmon.out").c_str(), 1);
141 JavaVM *jvm = app_global->activity->vm;
142 jvm->DetachCurrentThread();
145 static std::string javaStringToUTF8(jstring js)
148 // Get string as a UTF-8 c-string
149 const char *c_str = jnienv->GetStringUTFChars(js, nullptr);
152 // And free the c-string
153 jnienv->ReleaseStringUTFChars(js, c_str);
157 // Calls static method if obj is NULL
158 static std::string getAndroidPath(
159 jclass cls, jobject obj, jmethodID mt_getAbsPath, const char *getter)
164 mt_getter = jnienv->GetMethodID(cls, getter, "()Ljava/io/File;");
166 mt_getter = jnienv->GetStaticMethodID(cls, getter, "()Ljava/io/File;");
171 ob_file = jnienv->CallObjectMethod(obj, mt_getter);
173 ob_file = jnienv->CallStaticObjectMethod(cls, mt_getter);
175 // Call getAbsolutePath
176 auto js_path = (jstring)jnienv->CallObjectMethod(ob_file, mt_getAbsPath);
178 return javaStringToUTF8(js_path);
181 void initializePathsAndroid()
183 // Get Environment class
184 jclass cls_Env = jnienv->FindClass("android/os/Environment");
186 jclass cls_File = jnienv->FindClass("java/io/File");
187 // Get getAbsolutePath method
188 jmethodID mt_getAbsPath = jnienv->GetMethodID(
189 cls_File, "getAbsolutePath", "()Ljava/lang/String;");
190 std::string path_storage = getAndroidPath(
191 cls_Env, nullptr, mt_getAbsPath, "getExternalStorageDirectory");
193 path_user = path_storage + DIR_DELIM + PROJECT_NAME_C;
194 path_share = path_storage + DIR_DELIM + PROJECT_NAME_C;
195 path_cache = getAndroidPath(nativeActivity, app_global->activity->clazz,
196 mt_getAbsPath, "getCacheDir");
200 void showInputDialog(const std::string &acceptButton, const std::string &hint,
201 const std::string ¤t, int editType)
203 jmethodID showdialog = jnienv->GetMethodID(nativeActivity, "showDialog",
204 "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;I)V");
206 FATAL_ERROR_IF(showdialog == nullptr, "porting::showInputDialog unable to find "
207 "java show dialog method");
209 jstring jacceptButton = jnienv->NewStringUTF(acceptButton.c_str());
210 jstring jhint = jnienv->NewStringUTF(hint.c_str());
211 jstring jcurrent = jnienv->NewStringUTF(current.c_str());
212 jint jeditType = editType;
214 jnienv->CallVoidMethod(app_global->activity->clazz, showdialog, jacceptButton,
215 jhint, jcurrent, jeditType);
218 void openURLAndroid(const std::string &url)
220 jmethodID url_open = jnienv->GetMethodID(
221 nativeActivity, "openURL", "(Ljava/lang/String;)V");
223 FATAL_ERROR_IF(url_open == nullptr,
224 "porting::openURLAndroid unable to find java openURL method");
226 jstring jurl = jnienv->NewStringUTF(url.c_str());
227 jnienv->CallVoidMethod(app_global->activity->clazz, url_open, jurl);
230 int getInputDialogState()
232 jmethodID dialogstate =
233 jnienv->GetMethodID(nativeActivity, "getDialogState", "()I");
235 FATAL_ERROR_IF(dialogstate == nullptr, "porting::getInputDialogState unable to "
236 "find java dialog state method");
238 return jnienv->CallIntMethod(app_global->activity->clazz, dialogstate);
241 std::string getInputDialogValue()
243 jmethodID dialogvalue = jnienv->GetMethodID(
244 nativeActivity, "getDialogValue", "()Ljava/lang/String;");
246 FATAL_ERROR_IF(dialogvalue == nullptr, "porting::getInputDialogValue unable to "
247 "find java dialog value method");
249 jobject result = jnienv->CallObjectMethod(
250 app_global->activity->clazz, dialogvalue);
252 const char *javachars = jnienv->GetStringUTFChars((jstring)result, nullptr);
253 std::string text(javachars);
254 jnienv->ReleaseStringUTFChars((jstring)result, javachars);
260 float getDisplayDensity()
262 static bool firstrun = true;
263 static float value = 0;
266 jmethodID getDensity =
267 jnienv->GetMethodID(nativeActivity, "getDensity", "()F");
269 FATAL_ERROR_IF(getDensity == nullptr, "porting::getDisplayDensity unable "
270 "to find java getDensity method");
272 value = jnienv->CallFloatMethod(app_global->activity->clazz, getDensity);
278 v2u32 getDisplaySize()
280 static bool firstrun = true;
284 jmethodID getDisplayWidth = jnienv->GetMethodID(
285 nativeActivity, "getDisplayWidth", "()I");
287 FATAL_ERROR_IF(getDisplayWidth == nullptr,
288 "porting::getDisplayWidth unable to find java "
289 "getDisplayWidth method");
291 retval.X = jnienv->CallIntMethod(
292 app_global->activity->clazz, getDisplayWidth);
294 jmethodID getDisplayHeight = jnienv->GetMethodID(
295 nativeActivity, "getDisplayHeight", "()I");
297 FATAL_ERROR_IF(getDisplayHeight == nullptr,
298 "porting::getDisplayHeight unable to find java "
299 "getDisplayHeight method");
301 retval.Y = jnienv->CallIntMethod(
302 app_global->activity->clazz, getDisplayHeight);
308 #endif // ndef SERVER