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");
51 char *argv[] = {strdup(PROJECT_NAME), NULL};
52 main(ARRLEN(argv) - 1, argv);
54 } catch (std::exception &e) {
55 errorstream << "Uncaught exception in main thread: " << e.what() << std::endl;
58 errorstream << "Uncaught exception in main thread!" << std::endl;
62 porting::cleanupAndroid();
63 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, no idea why but there's a workaround */
70 /* for it right now */
72 JNIEXPORT void JNICALL Java_net_minetest_MtNativeActivity_putMessageBoxResult(
73 JNIEnv * env, jclass thiz, jstring text)
75 errorstream << "Java_net_minetest_MtNativeActivity_putMessageBoxResult got: "
76 << std::string((const char*)env->GetStringChars(text,0))
83 std::string path_storage = DIR_DELIM "sdcard" DIR_DELIM;
85 android_app* app_global;
87 jclass nativeActivity;
89 jclass findClass(std::string classname)
95 jclass nativeactivity = jnienv->FindClass("android/app/NativeActivity");
96 jmethodID getClassLoader =
97 jnienv->GetMethodID(nativeactivity,"getClassLoader",
98 "()Ljava/lang/ClassLoader;");
100 jnienv->CallObjectMethod(app_global->activity->clazz, getClassLoader);
101 jclass classLoader = jnienv->FindClass("java/lang/ClassLoader");
102 jmethodID findClass =
103 jnienv->GetMethodID(classLoader, "loadClass",
104 "(Ljava/lang/String;)Ljava/lang/Class;");
105 jstring strClassName =
106 jnienv->NewStringUTF(classname.c_str());
107 return (jclass) jnienv->CallObjectMethod(cls, findClass, strClassName);
112 jmethodID assetcopy = jnienv->GetMethodID(nativeActivity,"copyAssets","()V");
114 if (assetcopy == 0) {
115 assert("porting::copyAssets unable to find copy assets method" == 0);
118 jnienv->CallVoidMethod(app_global->activity->clazz, assetcopy);
123 porting::jnienv = NULL;
124 JavaVM *jvm = app_global->activity->vm;
125 JavaVMAttachArgs lJavaVMAttachArgs;
126 lJavaVMAttachArgs.version = JNI_VERSION_1_6;
127 lJavaVMAttachArgs.name = PROJECT_NAME_C "NativeThread";
128 lJavaVMAttachArgs.group = NULL;
130 // This is a ugly hack as arm v7a non debuggable builds crash without this
131 // printf ... if someone finds out why please fix it!
132 infostream << "Attaching native thread. " << std::endl;
134 if ( jvm->AttachCurrentThread(&porting::jnienv, &lJavaVMAttachArgs) == JNI_ERR) {
135 errorstream << "Failed to attach native thread to jvm" << std::endl;
139 nativeActivity = findClass("net/minetest/minetest/MtNativeActivity");
140 if (nativeActivity == 0) {
142 "porting::initAndroid unable to find java native activity class" <<
147 /* in the start-up code */
148 __android_log_print(ANDROID_LOG_ERROR, PROJECT_NAME_C,
149 "Initializing GPROF profiler");
150 monstartup("libminetest.so");
154 void cleanupAndroid()
158 errorstream << "Shutting down GPROF profiler" << std::endl;
159 setenv("CPUPROFILE", (path_user + DIR_DELIM + "gmon.out").c_str(), 1);
163 JavaVM *jvm = app_global->activity->vm;
164 jvm->DetachCurrentThread();
167 void setExternalStorageDir(JNIEnv* lJNIEnv)
169 // Android: Retrieve ablsolute path to external storage device (sdcard)
170 jclass ClassEnv = lJNIEnv->FindClass("android/os/Environment");
171 jmethodID MethodDir =
172 lJNIEnv->GetStaticMethodID(ClassEnv,
173 "getExternalStorageDirectory","()Ljava/io/File;");
174 jobject ObjectFile = lJNIEnv->CallStaticObjectMethod(ClassEnv, MethodDir);
175 jclass ClassFile = lJNIEnv->FindClass("java/io/File");
177 jmethodID MethodPath =
178 lJNIEnv->GetMethodID(ClassFile, "getAbsolutePath",
179 "()Ljava/lang/String;");
181 (jstring) lJNIEnv->CallObjectMethod(ObjectFile, MethodPath);
183 const char *externalPath = lJNIEnv->GetStringUTFChars(StringPath, NULL);
184 std::string userPath(externalPath);
185 lJNIEnv->ReleaseStringUTFChars(StringPath, externalPath);
187 path_storage = userPath;
188 path_user = userPath + DIR_DELIM + PROJECT_NAME_C;
189 path_share = userPath + DIR_DELIM + PROJECT_NAME_C;
192 void showInputDialog(const std::string& acceptButton, const std::string& hint,
193 const std::string& current, int editType)
195 jmethodID showdialog = jnienv->GetMethodID(nativeActivity,"showDialog",
196 "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;I)V");
198 if (showdialog == 0) {
199 assert("porting::showInputDialog unable to find java show dialog method" == 0);
202 jstring jacceptButton = jnienv->NewStringUTF(acceptButton.c_str());
203 jstring jhint = jnienv->NewStringUTF(hint.c_str());
204 jstring jcurrent = jnienv->NewStringUTF(current.c_str());
205 jint jeditType = editType;
207 jnienv->CallVoidMethod(app_global->activity->clazz, showdialog,
208 jacceptButton, jhint, jcurrent, jeditType);
211 int getInputDialogState()
213 jmethodID dialogstate = jnienv->GetMethodID(nativeActivity,
214 "getDialogState", "()I");
216 if (dialogstate == 0) {
217 assert("porting::getInputDialogState unable to find java dialog state method" == 0);
220 return jnienv->CallIntMethod(app_global->activity->clazz, dialogstate);
223 std::string getInputDialogValue()
225 jmethodID dialogvalue = jnienv->GetMethodID(nativeActivity,
226 "getDialogValue", "()Ljava/lang/String;");
228 if (dialogvalue == 0) {
229 assert("porting::getInputDialogValue unable to find java dialog value method" == 0);
232 jobject result = jnienv->CallObjectMethod(app_global->activity->clazz,
235 const char* javachars = jnienv->GetStringUTFChars((jstring) result,0);
236 std::string text(javachars);
237 jnienv->ReleaseStringUTFChars((jstring) result, javachars);
243 float getDisplayDensity()
245 static bool firstrun = true;
246 static float value = 0;
249 jmethodID getDensity = jnienv->GetMethodID(nativeActivity, "getDensity",
252 if (getDensity == 0) {
253 assert("porting::getDisplayDensity unable to find java getDensity method" == 0);
256 value = jnienv->CallFloatMethod(app_global->activity->clazz, getDensity);
262 v2u32 getDisplaySize()
264 static bool firstrun = true;
268 jmethodID getDisplayWidth = jnienv->GetMethodID(nativeActivity,
269 "getDisplayWidth", "()I");
271 if (getDisplayWidth == 0) {
272 assert("porting::getDisplayWidth unable to find java getDisplayWidth method" == 0);
275 retval.X = jnienv->CallIntMethod(app_global->activity->clazz,
278 jmethodID getDisplayHeight = jnienv->GetMethodID(nativeActivity,
279 "getDisplayHeight", "()I");
281 if (getDisplayHeight == 0) {
282 assert("porting::getDisplayHeight unable to find java getDisplayHeight method" == 0);
285 retval.Y = jnienv->CallIntMethod(app_global->activity->clazz,
292 #endif // ndef SERVER