(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_timer.h>
#if defined(__ANDROID__)
#include <android/log.h>
#endif
#if defined(__EMSCRIPTEN__)
#include <emscripten.h>
#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()};

View file

@ -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<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 Utils

View file

@ -15,6 +15,10 @@
#include <windows.h>
#endif
#if defined(__ANDROID__)
#include <jni.h>
#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;