Skip to content

Commit

Permalink
Don't crash if file-picker fails
Browse files Browse the repository at this point in the history
  • Loading branch information
ChristopherHX committed Nov 27, 2020
1 parent eebb7c8 commit 666ad06
Showing 1 changed file with 16 additions and 8 deletions.
24 changes: 16 additions & 8 deletions src/jni/main_activity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <mach/mach.h>
#endif
#include <file_picker_factory.h>
#include <game_window_manager.h>
#include "uuid.h"

FakeJni::JInt BuildVersion::SDK_INT = 27;
Expand Down Expand Up @@ -86,14 +87,21 @@ FakeJni::JLong MainActivity::getAvailableMemory() {
}

void MainActivity::pickImage(FakeJni::JLong callback) {
auto picker = FilePickerFactory::createFilePicker();
picker->setTitle("Select image");
picker->setFileNameFilters({ "*.png" });
if (picker->show()) {
auto method = getClass().getMethod("(JLjava/lang/String;)V", "nativeOnPickImageSuccess");
FakeJni::LocalFrame frame;
method->invoke(frame.getJniEnv(), this, callback, frame.getJniEnv().createLocalReference(std::make_shared<FakeJni::JString>(picker->getPickedFile())));
} else {
try {
auto picker = FilePickerFactory::createFilePicker();
picker->setTitle("Select image");
picker->setFileNameFilters({ "*.png" });
if (picker->show()) {
auto method = getClass().getMethod("(JLjava/lang/String;)V", "nativeOnPickImageSuccess");
FakeJni::LocalFrame frame;
method->invoke(frame.getJniEnv(), this, callback, frame.getJniEnv().createLocalReference(std::make_shared<FakeJni::JString>(picker->getPickedFile())));
} else {
auto method = getClass().getMethod("(J)V", "nativeOnPickImageCanceled");
FakeJni::LocalFrame frame;
method->invoke(frame.getJniEnv(), this, callback);
}
} catch(const std::exception& e) {
GameWindowManager::getManager()->getErrorHandler()->onError("FilePickerFactory", std::string("Failed to open the file-picker details: ") + e.what());
auto method = getClass().getMethod("(J)V", "nativeOnPickImageCanceled");
FakeJni::LocalFrame frame;
method->invoke(frame.getJniEnv(), this, callback);
Expand Down

0 comments on commit 666ad06

Please sign in to comment.