Skip to content

Commit 4f9a5f6

Browse files
author
Maksim
authoredMay 6, 2020
Android: porting_android.cpp refactoring (#9687)
* Android: porting_android.cpp refactoring * Replace assert to FATAL_ERROR_IF
1 parent e8e5d28 commit 4f9a5f6

File tree

2 files changed

+62
-87
lines changed

2 files changed

+62
-87
lines changed
 

Diff for: ‎src/porting_android.cpp

+56-79
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,7 @@ void android_main(android_app *app)
4747
Thread::setName("Main");
4848

4949
try {
50-
app_dummy();
51-
char *argv[] = {strdup(PROJECT_NAME), NULL};
50+
char *argv[] = {strdup(PROJECT_NAME), nullptr};
5251
main(ARRLEN(argv) - 1, argv);
5352
free(argv[0]);
5453
} catch (std::exception &e) {
@@ -64,85 +63,73 @@ void android_main(android_app *app)
6463
exit(retval);
6564
}
6665

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 */
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+
*/
7171
extern "C" {
7272
JNIEXPORT void JNICALL Java_net_minetest_minetest_GameActivity_putMessageBoxResult(
73-
JNIEnv * env, jclass thiz, jstring text)
73+
JNIEnv *env, jclass thiz, jstring text)
7474
{
75-
errorstream << "Java_net_minetest_minetest_GameActivity_putMessageBoxResult got: "
76-
<< std::string((const char*)env->GetStringChars(text,0))
77-
<< std::endl;
75+
errorstream <<
76+
"Java_net_minetest_minetest_GameActivity_putMessageBoxResult got: " <<
77+
std::string((const char*) env->GetStringChars(text, nullptr)) << std::endl;
7878
}
7979
}
8080

8181
namespace porting {
82-
83-
std::string path_storage = DIR_DELIM "sdcard" DIR_DELIM;
84-
85-
android_app* app_global;
86-
JNIEnv* jnienv;
82+
android_app *app_global;
83+
JNIEnv *jnienv;
8784
jclass nativeActivity;
8885

89-
jclass findClass(std::string classname)
86+
jclass findClass(const std::string &classname)
9087
{
91-
if (jnienv == 0) {
92-
return 0;
93-
}
88+
if (jnienv == nullptr)
89+
return nullptr;
9490

9591
jclass nativeactivity = jnienv->FindClass("android/app/NativeActivity");
96-
jmethodID getClassLoader =
97-
jnienv->GetMethodID(nativeactivity,"getClassLoader",
98-
"()Ljava/lang/ClassLoader;");
99-
jobject cls =
100-
jnienv->CallObjectMethod(app_global->activity->clazz, getClassLoader);
92+
jmethodID getClassLoader = jnienv->GetMethodID(
93+
nativeactivity, "getClassLoader", "()Ljava/lang/ClassLoader;");
94+
jobject cls = jnienv->CallObjectMethod(
95+
app_global->activity->clazz, getClassLoader);
10196
jclass classLoader = jnienv->FindClass("java/lang/ClassLoader");
102-
jmethodID findClass =
103-
jnienv->GetMethodID(classLoader, "loadClass",
97+
jmethodID findClass = jnienv->GetMethodID(classLoader, "loadClass",
10498
"(Ljava/lang/String;)Ljava/lang/Class;");
105-
jstring strClassName =
106-
jnienv->NewStringUTF(classname.c_str());
99+
jstring strClassName = jnienv->NewStringUTF(classname.c_str());
107100
return (jclass) jnienv->CallObjectMethod(cls, findClass, strClassName);
108101
}
109102

110103
void initAndroid()
111104
{
112-
porting::jnienv = NULL;
105+
porting::jnienv = nullptr;
113106
JavaVM *jvm = app_global->activity->vm;
114107
JavaVMAttachArgs lJavaVMAttachArgs;
115108
lJavaVMAttachArgs.version = JNI_VERSION_1_6;
116109
lJavaVMAttachArgs.name = PROJECT_NAME_C "NativeThread";
117-
lJavaVMAttachArgs.group = NULL;
118-
#ifdef NDEBUG
119-
// This is a ugly hack as arm v7a non debuggable builds crash without this
120-
// printf ... if someone finds out why please fix it!
121-
infostream << "Attaching native thread. " << std::endl;
122-
#endif
123-
if ( jvm->AttachCurrentThread(&porting::jnienv, &lJavaVMAttachArgs) == JNI_ERR) {
110+
lJavaVMAttachArgs.group = nullptr;
111+
112+
if (jvm->AttachCurrentThread(&porting::jnienv, &lJavaVMAttachArgs) == JNI_ERR) {
124113
errorstream << "Failed to attach native thread to jvm" << std::endl;
125114
exit(-1);
126115
}
127116

128117
nativeActivity = findClass("net/minetest/minetest/GameActivity");
129-
if (nativeActivity == 0) {
118+
if (nativeActivity == nullptr)
130119
errorstream <<
131120
"porting::initAndroid unable to find java native activity class" <<
132121
std::endl;
133-
}
134122

135123
#ifdef GPROF
136-
/* in the start-up code */
124+
// in the start-up code
137125
__android_log_print(ANDROID_LOG_ERROR, PROJECT_NAME_C,
138126
"Initializing GPROF profiler");
139-
monstartup("libminetest.so");
127+
monstartup("libMinetest.so");
140128
#endif
141129
}
142130

143131
void cleanupAndroid()
144132
{
145-
146133
#ifdef GPROF
147134
errorstream << "Shutting down GPROF profiler" << std::endl;
148135
setenv("CPUPROFILE", (path_user + DIR_DELIM + "gmon.out").c_str(), 1);
@@ -157,7 +144,7 @@ static std::string javaStringToUTF8(jstring js)
157144
{
158145
std::string str;
159146
// Get string as a UTF-8 c-string
160-
const char *c_str = jnienv->GetStringUTFChars(js, NULL);
147+
const char *c_str = jnienv->GetStringUTFChars(js, nullptr);
161148
// Save it
162149
str = c_str;
163150
// And free the c-string
@@ -166,17 +153,15 @@ static std::string javaStringToUTF8(jstring js)
166153
}
167154

168155
// Calls static method if obj is NULL
169-
static std::string getAndroidPath(jclass cls, jobject obj, jclass cls_File,
170-
jmethodID mt_getAbsPath, const char *getter)
156+
static std::string getAndroidPath(
157+
jclass cls, jobject obj, jmethodID mt_getAbsPath, const char *getter)
171158
{
172159
// Get getter method
173160
jmethodID mt_getter;
174161
if (obj)
175-
mt_getter = jnienv->GetMethodID(cls, getter,
176-
"()Ljava/io/File;");
162+
mt_getter = jnienv->GetMethodID(cls, getter, "()Ljava/io/File;");
177163
else
178-
mt_getter = jnienv->GetStaticMethodID(cls, getter,
179-
"()Ljava/io/File;");
164+
mt_getter = jnienv->GetStaticMethodID(cls, getter, "()Ljava/io/File;");
180165

181166
// Call getter
182167
jobject ob_file;
@@ -186,8 +171,7 @@ static std::string getAndroidPath(jclass cls, jobject obj, jclass cls_File,
186171
ob_file = jnienv->CallStaticObjectMethod(cls, mt_getter);
187172

188173
// Call getAbsolutePath
189-
jstring js_path = (jstring) jnienv->CallObjectMethod(ob_file,
190-
mt_getAbsPath);
174+
auto js_path = (jstring) jnienv->CallObjectMethod(ob_file, mt_getAbsPath);
191175

192176
return javaStringToUTF8(js_path);
193177
}
@@ -201,26 +185,24 @@ void initializePathsAndroid()
201185
// Get getAbsolutePath method
202186
jmethodID mt_getAbsPath = jnienv->GetMethodID(cls_File,
203187
"getAbsolutePath", "()Ljava/lang/String;");
188+
std::string path_storage = getAndroidPath(cls_Env, nullptr,
189+
mt_getAbsPath, "getExternalStorageDirectory");
204190

205-
path_cache = getAndroidPath(nativeActivity, app_global->activity->clazz,
206-
cls_File, mt_getAbsPath, "getCacheDir");
207-
path_storage = getAndroidPath(cls_Env, NULL, cls_File, mt_getAbsPath,
208-
"getExternalStorageDirectory");
209191
path_user = path_storage + DIR_DELIM + PROJECT_NAME_C;
210192
path_share = path_storage + DIR_DELIM + PROJECT_NAME_C;
211-
193+
path_cache = getAndroidPath(nativeActivity,
194+
app_global->activity->clazz, mt_getAbsPath, "getCacheDir");
212195
migrateCachePath();
213196
}
214197

215-
void showInputDialog(const std::string& acceptButton, const std::string& hint,
216-
const std::string& current, int editType)
198+
void showInputDialog(const std::string &acceptButton, const std::string &hint,
199+
const std::string &current, int editType)
217200
{
218-
jmethodID showdialog = jnienv->GetMethodID(nativeActivity,"showDialog",
201+
jmethodID showdialog = jnienv->GetMethodID(nativeActivity, "showDialog",
219202
"(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;I)V");
220203

221-
if (showdialog == 0) {
222-
assert("porting::showInputDialog unable to find java show dialog method" == 0);
223-
}
204+
FATAL_ERROR_IF(showdialog == nullptr,
205+
"porting::showInputDialog unable to find java show dialog method");
224206

225207
jstring jacceptButton = jnienv->NewStringUTF(acceptButton.c_str());
226208
jstring jhint = jnienv->NewStringUTF(hint.c_str());
@@ -236,9 +218,8 @@ int getInputDialogState()
236218
jmethodID dialogstate = jnienv->GetMethodID(nativeActivity,
237219
"getDialogState", "()I");
238220

239-
if (dialogstate == 0) {
240-
assert("porting::getInputDialogState unable to find java dialog state method" == 0);
241-
}
221+
FATAL_ERROR_IF(dialogstate == nullptr,
222+
"porting::getInputDialogState unable to find java dialog state method");
242223

243224
return jnienv->CallIntMethod(app_global->activity->clazz, dialogstate);
244225
}
@@ -248,14 +229,13 @@ std::string getInputDialogValue()
248229
jmethodID dialogvalue = jnienv->GetMethodID(nativeActivity,
249230
"getDialogValue", "()Ljava/lang/String;");
250231

251-
if (dialogvalue == 0) {
252-
assert("porting::getInputDialogValue unable to find java dialog value method" == 0);
253-
}
232+
FATAL_ERROR_IF(dialogvalue == nullptr,
233+
"porting::getInputDialogValue unable to find java dialog value method");
254234

255235
jobject result = jnienv->CallObjectMethod(app_global->activity->clazz,
256236
dialogvalue);
257237

258-
const char* javachars = jnienv->GetStringUTFChars((jstring) result,0);
238+
const char *javachars = jnienv->GetStringUTFChars((jstring) result, nullptr);
259239
std::string text(javachars);
260240
jnienv->ReleaseStringUTFChars((jstring) result, javachars);
261241

@@ -269,12 +249,11 @@ float getDisplayDensity()
269249
static float value = 0;
270250

271251
if (firstrun) {
272-
jmethodID getDensity = jnienv->GetMethodID(nativeActivity, "getDensity",
273-
"()F");
252+
jmethodID getDensity = jnienv->GetMethodID(nativeActivity,
253+
"getDensity", "()F");
274254

275-
if (getDensity == 0) {
276-
assert("porting::getDisplayDensity unable to find java getDensity method" == 0);
277-
}
255+
FATAL_ERROR_IF(getDensity == nullptr,
256+
"porting::getDisplayDensity unable to find java getDensity method");
278257

279258
value = jnienv->CallFloatMethod(app_global->activity->clazz, getDensity);
280259
firstrun = false;
@@ -291,19 +270,17 @@ v2u32 getDisplaySize()
291270
jmethodID getDisplayWidth = jnienv->GetMethodID(nativeActivity,
292271
"getDisplayWidth", "()I");
293272

294-
if (getDisplayWidth == 0) {
295-
assert("porting::getDisplayWidth unable to find java getDisplayWidth method" == 0);
296-
}
273+
FATAL_ERROR_IF(getDisplayWidth == nullptr,
274+
"porting::getDisplayWidth unable to find java getDisplayWidth method");
297275

298276
retval.X = jnienv->CallIntMethod(app_global->activity->clazz,
299277
getDisplayWidth);
300278

301279
jmethodID getDisplayHeight = jnienv->GetMethodID(nativeActivity,
302280
"getDisplayHeight", "()I");
303281

304-
if (getDisplayHeight == 0) {
305-
assert("porting::getDisplayHeight unable to find java getDisplayHeight method" == 0);
306-
}
282+
FATAL_ERROR_IF(getDisplayHeight == nullptr,
283+
"porting::getDisplayHeight unable to find java getDisplayHeight method");
307284

308285
retval.Y = jnienv->CallIntMethod(app_global->activity->clazz,
309286
getDisplayHeight);

Diff for: ‎src/porting_android.h

+6-8
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,15 @@ with this program; if not, write to the Free Software Foundation, Inc.,
3030
#include <string>
3131

3232
namespace porting {
33-
/** java app **/
33+
// java app
3434
extern android_app *app_global;
3535

36-
/** java <-> c++ interaction interface **/
36+
// java <-> c++ interaction interface
3737
extern JNIEnv *jnienv;
3838

39-
/**
40-
* do initialization required on android only
41-
*/
39+
// do initialization required on android only
4240
void initAndroid();
41+
4342
void cleanupAndroid();
4443

4544
/**
@@ -56,8 +55,8 @@ void initializePathsAndroid();
5655
* @param editType type of texfield
5756
* (1==multiline text input; 2==single line text input; 3=password field)
5857
*/
59-
void showInputDialog(const std::string& acceptButton,
60-
const std::string& hint, const std::string& current, int editType);
58+
void showInputDialog(const std::string &acceptButton,
59+
const std::string &hint, const std::string &current, int editType);
6160

6261
/**
6362
* WORKAROUND for not working callbacks from java -> c++
@@ -75,5 +74,4 @@ std::string getInputDialogValue();
7574
float getDisplayDensity();
7675
v2u32 getDisplaySize();
7776
#endif
78-
7977
}

0 commit comments

Comments
 (0)
Please sign in to comment.