From a09e4096ac091f65ad23b365dc402f52cde54885 Mon Sep 17 00:00:00 2001 From: Leon Styhre Date: Sat, 25 Nov 2023 10:31:09 +0100 Subject: [PATCH] (Android) Added preliminary support for requesting storage permissions --- es-app/src/main.cpp | 38 ++++++++++++++++++++++-------- es-core/src/utils/PlatformUtil.cpp | 17 +++++++++++++ es-core/src/utils/PlatformUtil.h | 11 +++++++++ 3 files changed, 56 insertions(+), 10 deletions(-) diff --git a/es-app/src/main.cpp b/es-app/src/main.cpp index fa6ef6c32..477c14dbd 100644 --- a/es-app/src/main.cpp +++ b/es-app/src/main.cpp @@ -42,10 +42,6 @@ #include #include -#if defined(__ANDROID__) -#include -#endif - #if defined(__EMSCRIPTEN__) #include #endif @@ -455,13 +451,22 @@ bool checkApplicationHomeDirectory() #if defined(_WIN64) std::cout << "First startup, creating application home directory \"" << Utils::String::replace(applicationHome, "/", "\\") << "\"\n"; +#elif defined(__ANDROID__) + __android_log_print(ANDROID_LOG_VERBOSE, nullptr, + "First startup, creating application home directory \"%s\"", + applicationHome.c_str()); #else std::cout << "First startup, creating application home directory \"" << applicationHome << "\"\n"; #endif Utils::FileSystem::createDirectory(applicationHome); if (!Utils::FileSystem::exists(applicationHome)) { +#if defined(__ANDROID__) + __android_log_print(ANDROID_LOG_ERROR, nullptr, + "Error: Couldn't create directory, permission problems?"); +#else std::cerr << "Error: Couldn't create directory, permission problems?\n"; +#endif return false; } } @@ -529,12 +534,6 @@ int main(int argc, char* argv[]) { const auto applicationStartTime {std::chrono::system_clock::now()}; -#if defined(__ANDROID__) - __android_log_print(ANDROID_LOG_VERBOSE, nullptr, "ES-DE running on Android!"); - SDL_Delay(3000); - return 0; -#endif - std::locale::global(std::locale("C")); #if defined(__APPLE__) @@ -737,6 +736,13 @@ int main(int argc, char* argv[]) return 1; } +#if defined(__ANDROID__) + LOG(LogDebug) << "Android API level: " << SDL_GetAndroidSDKVersion(); + LOG(LogDebug) << "Android storage state: " << SDL_AndroidGetExternalStorageState(); + LOG(LogDebug) << "Android internal path: " << SDL_AndroidGetInternalStoragePath(); + LOG(LogDebug) << "Android external path: " << SDL_AndroidGetExternalStoragePath(); +#endif + #if defined(APPLICATION_UPDATER) if (!noUpdateCheck) ApplicationUpdater::getInstance().checkForUpdates(); @@ -776,6 +782,18 @@ int main(int argc, char* argv[]) SDL_SetHint(SDL_HINT_ENABLE_SCREEN_KEYBOARD, "0"); } +#if defined(__ANDROID__) + Utils::Platform::Android::requestStoragePermission(); + + const std::string storageDir {"/sdcard/ES-DE"}; + if (!Utils::FileSystem::exists(storageDir)) { + LOG(LogInfo) << "Creating data directory \"" << storageDir << "\"..."; + if (!Utils::FileSystem::createDirectory(storageDir)) { + LOG(LogError) << "Couldn't create directory, permission problems?"; + } + } +#endif + MameNames::getInstance(); ThemeData::populateThemes(); loadSystemsReturnCode loadSystemsStatus {loadSystemConfigFile()}; diff --git a/es-core/src/utils/PlatformUtil.cpp b/es-core/src/utils/PlatformUtil.cpp index af8dd9e5a..8b00443c4 100644 --- a/es-core/src/utils/PlatformUtil.cpp +++ b/es-core/src/utils/PlatformUtil.cpp @@ -371,6 +371,23 @@ namespace Utils exit(EXIT_FAILURE); } +#if defined(__ANDROID__) + namespace Android + { + bool requestStoragePermission() + { + // TODO: Wait for interface to close and check actual outcome. + JNIEnv* jniEnv {reinterpret_cast(SDL_AndroidGetJNIEnv())}; + jclass jniClass {jniEnv->FindClass("org/esde/esde/esde")}; + jmethodID funcID { + jniEnv->GetStaticMethodID(jniClass, "requestStoragePermissions", "()Z")}; + const bool result { + static_cast(jniEnv->CallStaticBooleanMethod(jniClass, funcID))}; + return result; + } + + } // namespace Android +#endif } // namespace Platform } // namespace Utils diff --git a/es-core/src/utils/PlatformUtil.h b/es-core/src/utils/PlatformUtil.h index 6ff1b550e..a0f9a3fe4 100644 --- a/es-core/src/utils/PlatformUtil.h +++ b/es-core/src/utils/PlatformUtil.h @@ -15,6 +15,10 @@ #include #endif +#if defined(__ANDROID__) +#include +#endif + namespace Utils { namespace Platform @@ -52,6 +56,13 @@ namespace Utils // Immediately shut down the application as cleanly as possible. void emergencyShutdown(); +#if defined(__ANDROID__) + namespace Android + { + bool requestStoragePermission(); + }; // namespace Android +#endif + static QuitMode sQuitMode = QuitMode::QUIT; // This is simply to get rid of a GCC false positive -Wunused-variable compiler warning. static QuitMode sQuitModeDummy = sQuitMode;