Skip to content

Commit 97e233e

Browse files
committed
get working folder from android java class
1 parent a103538 commit 97e233e

5 files changed

Lines changed: 75 additions & 37 deletions

File tree

code/cfile/cfile.cpp

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
#include "osapi/osapi.h"
3333
#include "parse/encrypt.h"
3434
#include "cfilesystem.h"
35-
#include <SDL_system.h>
3635

3736
#include <limits>
3837

@@ -188,24 +187,8 @@ int cfile_init(const char *exe_dir, const char *cdrom_dir)
188187
#ifndef __ANDROID__
189188
strncpy(buf, exe_dir, CFILE_ROOT_DIRECTORY_LEN - 1);
190189
#else
191-
//Android gets the working folder path from the cmdline argument, if not present fallback to SDL path
192190
(void)exe_dir;
193-
extern char* Cmdline_working_folder;
194-
if (Cmdline_working_folder != nullptr)
195-
{
196-
strncpy(buf, Cmdline_working_folder, CFILE_ROOT_DIRECTORY_LEN - 1);
197-
}
198-
else
199-
{
200-
// fallback
201-
const char* android_path = SDL_AndroidGetExternalStoragePath();
202-
if (android_path == nullptr) {
203-
os::dialogs::Message(os::dialogs::MESSAGEBOX_ERROR,
204-
"Freespace Open needs permission to access the external storage.");
205-
return 1;
206-
}
207-
snprintf(buf, CFILE_ROOT_DIRECTORY_LEN, "%s/files", android_path);
208-
}
191+
strncpy(buf, os_get_working_folder_path().c_str(), CFILE_ROOT_DIRECTORY_LEN - 1);
209192
#endif
210193

211194
buf[CFILE_ROOT_DIRECTORY_LEN - 1] = '\0';

code/cmdline/cmdline.cpp

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -242,9 +242,6 @@ Flag exe_params[] =
242242
#ifdef WIN32
243243
{ "-fix_registry", "Use a different registry path", true, 0, EASY_DEFAULT, "Troubleshoot", "http://www.hard-light.net/wiki/index.php/Command-Line_Reference#-fix_registry", },
244244
#endif
245-
#ifdef __ANDROID__
246-
{ "-working_folder", "Change working folder in Android", true, 0, EASY_DEFAULT, "Android", "http://www.hard-light.net/wiki/index.php/Command-Line_Reference#-working_folder", },
247-
#endif
248245

249246
//flag launcher text FSO on_flags off_flags category reference URL
250247
{ "-voicer", "Enable voice recognition", true, 0, EASY_DEFAULT, "Experimental", "http://www.hard-light.net/wiki/index.php/Command-Line_Reference#-voicer", },
@@ -586,12 +583,6 @@ cmdline_parm output_script_json_arg("-output_script_json", nullptr, AT_NONE); //
586583
cmdline_parm output_script_luastub_arg("-output_script_lua", nullptr, AT_NONE); // mjnmixael
587584
cmdline_parm generate_controlconfig_arg("-controlconfig_tbl", nullptr, AT_NONE);
588585

589-
// Android
590-
#ifdef __ANDROID__
591-
cmdline_parm wfolder_arg("-working_folder", "Sets the working folder when launching as a library in Android", AT_STRING, true);
592-
char* Cmdline_working_folder = nullptr;
593-
#endif
594-
595586
// Deprecated flags - CommanderDJ
596587
cmdline_parm deprecated_spec_arg("-spec", "Deprecated", AT_NONE);
597588
cmdline_parm deprecated_glow_arg("-glow", "Deprecated", AT_NONE);
@@ -2422,12 +2413,6 @@ bool SetCmdlineParams()
24222413
Cmdline_multithreading = abs(multithreading.get_int());
24232414
}
24242415

2425-
#ifdef __ANDROID__
2426-
if (wfolder_arg.found()) {
2427-
Cmdline_working_folder = wfolder_arg.str();
2428-
}
2429-
#endif
2430-
24312416
return true;
24322417
}
24332418

code/cmdline/cmdline.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -169,8 +169,4 @@ extern void removeWindowModeOption();
169169
extern void removeResolutionOption();
170170
extern void removeResolutionVROption();
171171

172-
//Android Exclusive
173-
#ifdef __ANDROID__
174-
extern char* Cmdline_working_folder;
175-
#endif
176172
#endif

code/osapi/osapi.cpp

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,12 @@
2828
#include <sys/types.h>
2929
#endif
3030

31+
#ifdef __ANDROID__
32+
#include <SDL_system.h>
33+
#include <SDL.h>
34+
#include <jni.h>
35+
#endif
36+
3137
namespace
3238
{
3339
const char* ORGANIZATION_NAME = "HardLightProductions";
@@ -840,3 +846,63 @@ SCP_string os_get_config_path(const SCP_string& subpath)
840846
return ss.str();
841847
}
842848

849+
/*
850+
Special functions for Android
851+
*/
852+
853+
#ifdef __ANDROID__
854+
SCP_string os_get_working_folder_path()
855+
{
856+
SCP_string wfp{};
857+
858+
//Get the JNI Environment pointer and current Activity instance via SDL
859+
JNIEnv* env = (JNIEnv*)SDL_AndroidGetJNIEnv();
860+
jobject activity = (jobject)SDL_AndroidGetActivity();
861+
862+
if (env && activity) {
863+
// Locate the Java class (GameActivity on KnossosNET)
864+
jclass ga = env->GetObjectClass(activity);
865+
if(ga) {
866+
// Get the methodID (activity, methodName, signature);
867+
// "()Ljava/lang/String;" means it takes no paramenters and returns a string
868+
jmethodID methodId = env->GetStaticMethodID(ga, "getWorkingFolder", "()Ljava/lang/String;");
869+
if (methodId) {
870+
jstring jString = (jstring)env->CallStaticObjectMethod(ga, methodId);
871+
if (jString) {
872+
const char* workingFolder = env->GetStringUTFChars(jString, 0);
873+
wfp = SCP_string(workingFolder);
874+
env->ReleaseStringUTFChars(jString, workingFolder);
875+
env->DeleteLocalRef(jString);
876+
} else {
877+
mprintf(("Couldn't get the jString.\n"));
878+
}
879+
} else {
880+
mprintf(("Couldn't get the methodID.\n"));
881+
}
882+
env->DeleteLocalRef(ga);
883+
} else {
884+
mprintf(("Couldn't get java class.\n"));
885+
}
886+
} else {
887+
mprintf(("Couldn't get JNI enviroment or activity.\n"));
888+
}
889+
890+
if (wfp.empty()) {
891+
mprintf(("Couldn't get working folder path from Java class, reverting to SDL default.\n"));
892+
// Fallback to app space on internal storage
893+
const char* fallbackPath = SDL_AndroidGetExternalStoragePath();
894+
if (fallbackPath) {
895+
wfp = SCP_string(fallbackPath);
896+
wfp += "/files/";
897+
}
898+
}
899+
900+
// Ensure path ends with a dir separator
901+
if (!wfp.empty() && (wfp.back() != DIR_SEPARATOR_CHAR)) {
902+
wfp += DIR_SEPARATOR_CHAR;
903+
}
904+
mprintf(("Using working folder: %s\n", wfp.c_str()));
905+
return wfp;
906+
}
907+
#endif
908+

code/osapi/osapi.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,14 @@ bool os_is_legacy_mode();
9191
*/
9292
SCP_string os_get_config_path(const SCP_string& subpath = "");
9393

94+
/*
95+
Special functions for Android
96+
*/
97+
#ifdef __ANDROID__
98+
// Get working folder absolute path from Android Java Class
99+
SCP_string os_get_working_folder_path();
100+
#endif
101+
94102
namespace os
95103
{
96104
/**

0 commit comments

Comments
 (0)