]> git.lizzy.rs Git - dragonfireclient.git/blob - src/porting_android.cpp
Android: Add support for sharing debug.txt (#12370)
[dragonfireclient.git] / src / porting_android.cpp
1 /*
2 Minetest
3 Copyright (C) 2014 celeron55, Perttu Ahola <celeron55@gmail.com>
4
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.
9
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.
14
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.
18 */
19
20 #ifndef __ANDROID__
21 #error This file may only be compiled for android!
22 #endif
23
24 #include "util/numeric.h"
25 #include "porting.h"
26 #include "porting_android.h"
27 #include "threading/thread.h"
28 #include "config.h"
29 #include "filesys.h"
30 #include "log.h"
31
32 #include <sstream>
33 #include <exception>
34 #include <cstdlib>
35
36 #ifdef GPROF
37 #include "prof.h"
38 #endif
39
40 extern int main(int argc, char *argv[]);
41
42 void android_main(android_app *app)
43 {
44         int retval = 0;
45         porting::app_global = app;
46
47         Thread::setName("Main");
48
49         try {
50                 char *argv[] = {strdup(PROJECT_NAME), nullptr};
51                 main(ARRLEN(argv) - 1, argv);
52                 free(argv[0]);
53         } catch (std::exception &e) {
54                 errorstream << "Uncaught exception in main thread: " << e.what() << std::endl;
55                 retval = -1;
56         } catch (...) {
57                 errorstream << "Uncaught exception in main thread!" << std::endl;
58                 retval = -1;
59         }
60
61         porting::cleanupAndroid();
62         infostream << "Shutting down." << std::endl;
63         exit(retval);
64 }
65
66 /**
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
70  */
71 extern "C" {
72         JNIEXPORT void JNICALL Java_net_minetest_minetest_GameActivity_putMessageBoxResult(
73                         JNIEnv *env, jclass thiz, jstring text)
74         {
75                 errorstream <<
76                         "Java_net_minetest_minetest_GameActivity_putMessageBoxResult got: " <<
77                         std::string((const char*) env->GetStringChars(text, nullptr)) << std::endl;
78         }
79 }
80
81 namespace porting {
82 android_app *app_global;
83 JNIEnv      *jnienv;
84 jclass       nativeActivity;
85
86 jclass findClass(const std::string &classname)
87 {
88         if (jnienv == nullptr)
89                 return nullptr;
90
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);
101 }
102
103 void initAndroid()
104 {
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;
111
112         if (jvm->AttachCurrentThread(&porting::jnienv, &lJavaVMAttachArgs) == JNI_ERR) {
113                 errorstream << "Failed to attach native thread to jvm" << std::endl;
114                 exit(-1);
115         }
116
117         nativeActivity = findClass("net/minetest/minetest/GameActivity");
118         if (nativeActivity == nullptr)
119                 errorstream <<
120                         "porting::initAndroid unable to find java native activity class" <<
121                         std::endl;
122
123 #ifdef GPROF
124         // in the start-up code
125         __android_log_print(ANDROID_LOG_ERROR, PROJECT_NAME_C,
126                         "Initializing GPROF profiler");
127         monstartup("libMinetest.so");
128 #endif
129 }
130
131 void cleanupAndroid()
132 {
133 #ifdef GPROF
134         errorstream << "Shutting down GPROF profiler" << std::endl;
135         setenv("CPUPROFILE", (path_user + DIR_DELIM + "gmon.out").c_str(), 1);
136         moncleanup();
137 #endif
138
139         JavaVM *jvm = app_global->activity->vm;
140         jvm->DetachCurrentThread();
141 }
142
143 static std::string javaStringToUTF8(jstring js)
144 {
145         std::string str;
146         // Get string as a UTF-8 c-string
147         const char *c_str = jnienv->GetStringUTFChars(js, nullptr);
148         // Save it
149         str = c_str;
150         // And free the c-string
151         jnienv->ReleaseStringUTFChars(js, c_str);
152         return str;
153 }
154
155 void initializePathsAndroid()
156 {
157         // Set user and share paths
158         {
159                 jmethodID getUserDataPath = jnienv->GetMethodID(nativeActivity,
160                                 "getUserDataPath", "()Ljava/lang/String;");
161                 FATAL_ERROR_IF(getUserDataPath==nullptr,
162                                 "porting::initializePathsAndroid unable to find Java getUserDataPath method");
163                 jobject result = jnienv->CallObjectMethod(app_global->activity->clazz, getUserDataPath);
164                 const char *javachars = jnienv->GetStringUTFChars((jstring) result, nullptr);
165                 path_user = javachars;
166                 path_share = javachars;
167                 path_locale  = path_share + DIR_DELIM + "locale";
168                 jnienv->ReleaseStringUTFChars((jstring) result, javachars);
169         }
170
171         // Set cache path
172         {
173                 jmethodID getCachePath = jnienv->GetMethodID(nativeActivity,
174                                 "getCachePath", "()Ljava/lang/String;");
175                 FATAL_ERROR_IF(getCachePath==nullptr,
176                                 "porting::initializePathsAndroid unable to find Java getCachePath method");
177                 jobject result = jnienv->CallObjectMethod(app_global->activity->clazz, getCachePath);
178                 const char *javachars = jnienv->GetStringUTFChars((jstring) result, nullptr);
179                 path_cache = javachars;
180                 jnienv->ReleaseStringUTFChars((jstring) result, javachars);
181
182                 migrateCachePath();
183         }
184 }
185
186 void showInputDialog(const std::string &acceptButton, const std::string &hint,
187                 const std::string &current, int editType)
188 {
189         jmethodID showdialog = jnienv->GetMethodID(nativeActivity, "showDialog",
190                 "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;I)V");
191
192         FATAL_ERROR_IF(showdialog == nullptr,
193                 "porting::showInputDialog unable to find java show dialog method");
194
195         jstring jacceptButton = jnienv->NewStringUTF(acceptButton.c_str());
196         jstring jhint         = jnienv->NewStringUTF(hint.c_str());
197         jstring jcurrent      = jnienv->NewStringUTF(current.c_str());
198         jint    jeditType     = editType;
199
200         jnienv->CallVoidMethod(app_global->activity->clazz, showdialog,
201                         jacceptButton, jhint, jcurrent, jeditType);
202 }
203
204 void openURIAndroid(const std::string &url)
205 {
206         jmethodID url_open = jnienv->GetMethodID(nativeActivity, "openURI",
207                 "(Ljava/lang/String;)V");
208
209         FATAL_ERROR_IF(url_open == nullptr,
210                 "porting::openURIAndroid unable to find java openURI method");
211
212         jstring jurl = jnienv->NewStringUTF(url.c_str());
213         jnienv->CallVoidMethod(app_global->activity->clazz, url_open, jurl);
214 }
215
216 void shareFileAndroid(const std::string &path)
217 {
218         jmethodID url_open = jnienv->GetMethodID(nativeActivity, "shareFile",
219                         "(Ljava/lang/String;)V");
220
221         FATAL_ERROR_IF(url_open == nullptr,
222                         "porting::shareFileAndroid unable to find java openURI method");
223
224         jstring jurl = jnienv->NewStringUTF(path.c_str());
225         jnienv->CallVoidMethod(app_global->activity->clazz, url_open, jurl);
226 }
227
228 int getInputDialogState()
229 {
230         jmethodID dialogstate = jnienv->GetMethodID(nativeActivity,
231                         "getDialogState", "()I");
232
233         FATAL_ERROR_IF(dialogstate == nullptr,
234                 "porting::getInputDialogState unable to find java dialog state method");
235
236         return jnienv->CallIntMethod(app_global->activity->clazz, dialogstate);
237 }
238
239 std::string getInputDialogValue()
240 {
241         jmethodID dialogvalue = jnienv->GetMethodID(nativeActivity,
242                         "getDialogValue", "()Ljava/lang/String;");
243
244         FATAL_ERROR_IF(dialogvalue == nullptr,
245                 "porting::getInputDialogValue unable to find java dialog value method");
246
247         jobject result = jnienv->CallObjectMethod(app_global->activity->clazz,
248                         dialogvalue);
249
250         const char *javachars = jnienv->GetStringUTFChars((jstring) result, nullptr);
251         std::string text(javachars);
252         jnienv->ReleaseStringUTFChars((jstring) result, javachars);
253
254         return text;
255 }
256
257 #ifndef SERVER
258 float getDisplayDensity()
259 {
260         static bool firstrun = true;
261         static float value = 0;
262
263         if (firstrun) {
264                 jmethodID getDensity = jnienv->GetMethodID(nativeActivity,
265                                 "getDensity", "()F");
266
267                 FATAL_ERROR_IF(getDensity == nullptr,
268                         "porting::getDisplayDensity unable to find java getDensity method");
269
270                 value = jnienv->CallFloatMethod(app_global->activity->clazz, getDensity);
271                 firstrun = false;
272         }
273         return value;
274 }
275
276 v2u32 getDisplaySize()
277 {
278         static bool firstrun = true;
279         static v2u32 retval;
280
281         if (firstrun) {
282                 jmethodID getDisplayWidth = jnienv->GetMethodID(nativeActivity,
283                                 "getDisplayWidth", "()I");
284
285                 FATAL_ERROR_IF(getDisplayWidth == nullptr,
286                         "porting::getDisplayWidth unable to find java getDisplayWidth method");
287
288                 retval.X = jnienv->CallIntMethod(app_global->activity->clazz,
289                                 getDisplayWidth);
290
291                 jmethodID getDisplayHeight = jnienv->GetMethodID(nativeActivity,
292                                 "getDisplayHeight", "()I");
293
294                 FATAL_ERROR_IF(getDisplayHeight == nullptr,
295                         "porting::getDisplayHeight unable to find java getDisplayHeight method");
296
297                 retval.Y = jnienv->CallIntMethod(app_global->activity->clazz,
298                                 getDisplayHeight);
299
300                 firstrun = false;
301         }
302         return retval;
303 }
304 #endif // ndef SERVER
305 }