Android: Use getExternalStorageDirectory() rather than hardcoding to /sdcard

This commit is contained in:
Connor McLaughlin 2020-08-02 00:25:12 +10:00
parent 0c1b637549
commit 7b384ad300
3 changed files with 19 additions and 8 deletions

View file

@ -58,9 +58,10 @@ std::string JStringToString(JNIEnv* env, jstring str)
} }
} // namespace AndroidHelpers } // namespace AndroidHelpers
AndroidHostInterface::AndroidHostInterface(jobject java_object, jobject context_object) AndroidHostInterface::AndroidHostInterface(jobject java_object, jobject context_object, std::string user_directory)
: m_java_object(java_object), m_settings_interface(context_object) : m_java_object(java_object), m_settings_interface(context_object)
{ {
m_user_directory = std::move(user_directory);
} }
AndroidHostInterface::~AndroidHostInterface() AndroidHostInterface::~AndroidHostInterface()
@ -124,8 +125,8 @@ float AndroidHostInterface::GetFloatSettingValue(const char* section, const char
void AndroidHostInterface::SetUserDirectory() void AndroidHostInterface::SetUserDirectory()
{ {
// TODO: Should this be customizable or use an API-determined path? // Already set in constructor.
m_user_directory = "/sdcard/duckstation"; Assert(!m_user_directory.empty());
} }
void AndroidHostInterface::LoadSettings() void AndroidHostInterface::LoadSettings()
@ -455,7 +456,7 @@ extern "C" jint JNI_OnLoad(JavaVM* vm, void* reserved)
#define DEFINE_JNI_ARGS_METHOD(return_type, name, ...) \ #define DEFINE_JNI_ARGS_METHOD(return_type, name, ...) \
extern "C" JNIEXPORT return_type JNICALL Java_com_github_stenzek_duckstation_##name(JNIEnv* env, __VA_ARGS__) extern "C" JNIEXPORT return_type JNICALL Java_com_github_stenzek_duckstation_##name(JNIEnv* env, __VA_ARGS__)
DEFINE_JNI_ARGS_METHOD(jobject, AndroidHostInterface_create, jobject unused, jobject context_object) DEFINE_JNI_ARGS_METHOD(jobject, AndroidHostInterface_create, jobject unused, jobject context_object, jstring user_directory)
{ {
Log::SetDebugOutputParams(true, nullptr, LOGLEVEL_DEBUG); Log::SetDebugOutputParams(true, nullptr, LOGLEVEL_DEBUG);
@ -471,7 +472,8 @@ DEFINE_JNI_ARGS_METHOD(jobject, AndroidHostInterface_create, jobject unused, job
Assert(java_obj_ref != nullptr); Assert(java_obj_ref != nullptr);
// initialize the C++ side // initialize the C++ side
AndroidHostInterface* cpp_obj = new AndroidHostInterface(java_obj_ref, context_object); std::string user_directory_str = AndroidHelpers::JStringToString(env, user_directory);
AndroidHostInterface* cpp_obj = new AndroidHostInterface(java_obj_ref, context_object, std::move(user_directory_str));
if (!cpp_obj->Initialize()) if (!cpp_obj->Initialize())
{ {
// TODO: Do we need to release the original java object reference? // TODO: Do we need to release the original java object reference?

View file

@ -17,7 +17,7 @@ class Controller;
class AndroidHostInterface final : public CommonHostInterface class AndroidHostInterface final : public CommonHostInterface
{ {
public: public:
AndroidHostInterface(jobject java_object, jobject context_object); AndroidHostInterface(jobject java_object, jobject context_object, std::string user_directory);
~AndroidHostInterface() override; ~AndroidHostInterface() override;
bool Initialize() override; bool Initialize() override;

View file

@ -1,13 +1,15 @@
package com.github.stenzek.duckstation; package com.github.stenzek.duckstation;
import android.content.Context; import android.content.Context;
import android.os.Environment;
import android.util.Log;
import android.view.Surface; import android.view.Surface;
public class AndroidHostInterface public class AndroidHostInterface
{ {
private long nativePointer; private long nativePointer;
static public native AndroidHostInterface create(Context context); static public native AndroidHostInterface create(Context context, String userDirectory);
public AndroidHostInterface(long nativePointer) public AndroidHostInterface(long nativePointer)
{ {
@ -41,7 +43,14 @@ public class AndroidHostInterface
static private AndroidHostInterface mInstance; static private AndroidHostInterface mInstance;
static public boolean createInstance(Context context) { static public boolean createInstance(Context context) {
mInstance = create(context); // Set user path.
String externalStorageDirectory = Environment.getExternalStorageDirectory().getAbsolutePath();
if (externalStorageDirectory.isEmpty())
externalStorageDirectory = "/sdcard";
externalStorageDirectory += "/duckstation";
Log.i("AndroidHostInterface", "User directory: " + externalStorageDirectory);
mInstance = create(context, externalStorageDirectory);
return mInstance != null; return mInstance != null;
} }