(Android) Added preliminary support for requesting storage permissions

This commit is contained in:
Leon Styhre 2023-11-25 10:31:09 +01:00
parent 8c032d50b2
commit a09e4096ac
3 changed files with 56 additions and 10 deletions

View file

@ -42,10 +42,6 @@
#include <SDL2/SDL_main.h> #include <SDL2/SDL_main.h>
#include <SDL2/SDL_timer.h> #include <SDL2/SDL_timer.h>
#if defined(__ANDROID__)
#include <android/log.h>
#endif
#if defined(__EMSCRIPTEN__) #if defined(__EMSCRIPTEN__)
#include <emscripten.h> #include <emscripten.h>
#endif #endif
@ -455,13 +451,22 @@ bool checkApplicationHomeDirectory()
#if defined(_WIN64) #if defined(_WIN64)
std::cout << "First startup, creating application home directory \"" std::cout << "First startup, creating application home directory \""
<< Utils::String::replace(applicationHome, "/", "\\") << "\"\n"; << 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 #else
std::cout << "First startup, creating application home directory \"" << applicationHome std::cout << "First startup, creating application home directory \"" << applicationHome
<< "\"\n"; << "\"\n";
#endif #endif
Utils::FileSystem::createDirectory(applicationHome); Utils::FileSystem::createDirectory(applicationHome);
if (!Utils::FileSystem::exists(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"; std::cerr << "Error: Couldn't create directory, permission problems?\n";
#endif
return false; return false;
} }
} }
@ -529,12 +534,6 @@ int main(int argc, char* argv[])
{ {
const auto applicationStartTime {std::chrono::system_clock::now()}; 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")); std::locale::global(std::locale("C"));
#if defined(__APPLE__) #if defined(__APPLE__)
@ -737,6 +736,13 @@ int main(int argc, char* argv[])
return 1; 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 defined(APPLICATION_UPDATER)
if (!noUpdateCheck) if (!noUpdateCheck)
ApplicationUpdater::getInstance().checkForUpdates(); ApplicationUpdater::getInstance().checkForUpdates();
@ -776,6 +782,18 @@ int main(int argc, char* argv[])
SDL_SetHint(SDL_HINT_ENABLE_SCREEN_KEYBOARD, "0"); 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(); MameNames::getInstance();
ThemeData::populateThemes(); ThemeData::populateThemes();
loadSystemsReturnCode loadSystemsStatus {loadSystemConfigFile()}; loadSystemsReturnCode loadSystemsStatus {loadSystemConfigFile()};

View file

@ -371,6 +371,23 @@ namespace Utils
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
#if defined(__ANDROID__)
namespace Android
{
bool requestStoragePermission()
{
// TODO: Wait for interface to close and check actual outcome.
JNIEnv* jniEnv {reinterpret_cast<JNIEnv*>(SDL_AndroidGetJNIEnv())};
jclass jniClass {jniEnv->FindClass("org/esde/esde/esde")};
jmethodID funcID {
jniEnv->GetStaticMethodID(jniClass, "requestStoragePermissions", "()Z")};
const bool result {
static_cast<bool>(jniEnv->CallStaticBooleanMethod(jniClass, funcID))};
return result;
}
} // namespace Android
#endif
} // namespace Platform } // namespace Platform
} // namespace Utils } // namespace Utils

View file

@ -15,6 +15,10 @@
#include <windows.h> #include <windows.h>
#endif #endif
#if defined(__ANDROID__)
#include <jni.h>
#endif
namespace Utils namespace Utils
{ {
namespace Platform namespace Platform
@ -52,6 +56,13 @@ namespace Utils
// Immediately shut down the application as cleanly as possible. // Immediately shut down the application as cleanly as possible.
void emergencyShutdown(); void emergencyShutdown();
#if defined(__ANDROID__)
namespace Android
{
bool requestStoragePermission();
}; // namespace Android
#endif
static QuitMode sQuitMode = QuitMode::QUIT; static QuitMode sQuitMode = QuitMode::QUIT;
// This is simply to get rid of a GCC false positive -Wunused-variable compiler warning. // This is simply to get rid of a GCC false positive -Wunused-variable compiler warning.
static QuitMode sQuitModeDummy = sQuitMode; static QuitMode sQuitModeDummy = sQuitMode;