Make the new image selection path work on Android as well.

This commit is contained in:
Henrik Rydgård 2023-03-22 14:14:15 +01:00
parent e9f5394f59
commit 0403bbf084
5 changed files with 46 additions and 36 deletions

View file

@ -7,12 +7,14 @@ RequestManager g_requestManager;
const char *RequestTypeAsString(SystemRequestType type) { const char *RequestTypeAsString(SystemRequestType type) {
switch (type) { switch (type) {
case SystemRequestType::INPUT_TEXT_MODAL: return "INPUT_TEXT_MODAL"; case SystemRequestType::INPUT_TEXT_MODAL: return "INPUT_TEXT_MODAL";
case SystemRequestType::BROWSE_FOR_IMAGE: return "BROWSE_FOR_IMAGE";
default: return "N/A"; default: return "N/A";
} }
} }
bool RequestManager::MakeSystemRequest(SystemRequestType type, RequestCallback callback, const std::string &param1, const std::string &param2) { bool RequestManager::MakeSystemRequest(SystemRequestType type, RequestCallback callback, const std::string &param1, const std::string &param2) {
int requestId = idCounter_++; int requestId = idCounter_++;
INFO_LOG(SYSTEM, "Making system request %s: id %d, callback_valid %d", RequestTypeAsString(type), requestId, callback != nullptr);
if (!System_MakeRequest(type, requestId, param1, param2)) { if (!System_MakeRequest(type, requestId, param1, param2)) {
return false; return false;
} }
@ -23,6 +25,7 @@ bool RequestManager::MakeSystemRequest(SystemRequestType type, RequestCallback c
} }
std::lock_guard<std::mutex> guard(callbackMutex_); std::lock_guard<std::mutex> guard(callbackMutex_);
INFO_LOG(SYSTEM, "Registering pending callback %d", requestId);
callbackMap_[requestId] = callback; callbackMap_[requestId] = callback;
return true; return true;
} }
@ -41,6 +44,7 @@ void RequestManager::PostSystemSuccess(int requestId, const char *responseString
response.responseString = responseString; response.responseString = responseString;
response.responseValue = responseValue; response.responseValue = responseValue;
pendingResponses_.push_back(response); pendingResponses_.push_back(response);
INFO_LOG(SYSTEM, "PostSystemSuccess: Request %d (%s, %d)", requestId, responseString, responseValue);
} }
void RequestManager::PostSystemFailure(int requestId) { void RequestManager::PostSystemFailure(int requestId) {
@ -50,6 +54,7 @@ void RequestManager::PostSystemFailure(int requestId) {
ERROR_LOG(SYSTEM, "PostSystemFailure: Unexpected request ID %d", requestId); ERROR_LOG(SYSTEM, "PostSystemFailure: Unexpected request ID %d", requestId);
return; return;
} }
INFO_LOG(SYSTEM, "PostSystemFailure: Request %d failed", requestId);
callbackMap_.erase(iter); callbackMap_.erase(iter);
} }

View file

@ -490,7 +490,7 @@ void SetBackgroundPopupScreen::update() {
File::WriteStringToFile(false, pic->data, bgPng); File::WriteStringToFile(false, pic->data, bgPng);
} }
NativeMessageReceived("bgImage_updated", ""); UIBackgroundShutdown();
// It's worse if it flickers, stay open for at least 1s. // It's worse if it flickers, stay open for at least 1s.
timeDone_ = timeStart_ + 1.0; timeDone_ = timeStart_ + 1.0;

View file

@ -539,8 +539,6 @@ bool System_GetPropertyBool(SystemProperty prop) {
} }
void System_Notify(SystemNotification notification) { void System_Notify(SystemNotification notification) {
switch (notification) {
}
} }
std::string Android_GetInputDeviceDebugString() { std::string Android_GetInputDeviceDebugString() {
@ -1038,34 +1036,28 @@ bool System_MakeRequest(SystemRequestType type, int requestId, const std::string
return true; return true;
} }
case SystemRequestType::BROWSE_FOR_IMAGE: case SystemRequestType::BROWSE_FOR_IMAGE:
return false; PushCommand("browse_image", StringFromFormat("%d", requestId));
return true;
default: default:
return false; return false;
} }
} }
extern "C" void JNICALL Java_org_ppsspp_ppsspp_NativeApp_sendInputBox(JNIEnv *env, jclass, jstring jseqID, jboolean result, jstring jvalue) { extern "C" void JNICALL Java_org_ppsspp_ppsspp_NativeApp_sendRequestResult(JNIEnv *env, jclass, jint jrequestID, jboolean result, jstring jvalue, jint jintValue) {
std::string seqID = GetJavaString(env, jseqID);
std::string value = GetJavaString(env, jvalue); std::string value = GetJavaString(env, jvalue);
static std::string lastSeqID = ""; static jint lastSeqID = -1;
if (lastSeqID == seqID) { if (lastSeqID == jrequestID) {
// We send this on dismiss, so twice in many cases. // We send this on dismiss, so twice in many cases.
DEBUG_LOG(SYSTEM, "Ignoring duplicate sendInputBox"); WARN_LOG(SYSTEM, "Ignoring duplicate sendInputBox");
return;
}
lastSeqID = seqID;
int seq = 0;
if (!TryParse(seqID, &seq)) {
ERROR_LOG(SYSTEM, "Invalid inputbox seqID value: %s", seqID.c_str());
return; return;
} }
lastSeqID = jrequestID;
if (result) { if (result) {
g_requestManager.PostSystemSuccess(seq, value.c_str()); g_requestManager.PostSystemSuccess(jrequestID, value.c_str());
} else { } else {
g_requestManager.PostSystemFailure(seq); g_requestManager.PostSystemFailure(jrequestID);
} }
} }
@ -1227,7 +1219,7 @@ extern "C" void JNICALL Java_org_ppsspp_ppsspp_NativeApp_sendMessage(JNIEnv *env
} else if (msg == "sustained_perf_supported") { } else if (msg == "sustained_perf_supported") {
sustainedPerfSupported = true; sustainedPerfSupported = true;
} else if (msg == "safe_insets") { } else if (msg == "safe_insets") {
INFO_LOG(SYSTEM, "Got insets: %s", prm.c_str()); // INFO_LOG(SYSTEM, "Got insets: %s", prm.c_str());
// We don't bother with supporting exact rectangular regions. Safe insets are good enough. // We don't bother with supporting exact rectangular regions. Safe insets are good enough.
int left, right, top, bottom; int left, right, top, bottom;
if (4 == sscanf(prm.c_str(), "%d:%d:%d:%d", &left, &right, &top, &bottom)) { if (4 == sscanf(prm.c_str(), "%d:%d:%d:%d", &left, &right, &top, &bottom)) {

View file

@ -105,6 +105,8 @@ public abstract class NativeActivity extends Activity {
private static final int RESULT_OPEN_DOCUMENT = 2; private static final int RESULT_OPEN_DOCUMENT = 2;
private static final int RESULT_OPEN_DOCUMENT_TREE = 3; private static final int RESULT_OPEN_DOCUMENT_TREE = 3;
private int imageRequestId = -1;
// Allow for multiple connected gamepads but just consider them the same for now. // Allow for multiple connected gamepads but just consider them the same for now.
// Actually this is not entirely true, see the code. // Actually this is not entirely true, see the code.
private ArrayList<InputDeviceState> inputPlayers = new ArrayList<InputDeviceState>(); private ArrayList<InputDeviceState> inputPlayers = new ArrayList<InputDeviceState>();
@ -1130,15 +1132,16 @@ public abstract class NativeActivity extends Activity {
@Override @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) { protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data); super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RESULT_LOAD_IMAGE) {
if (resultCode != RESULT_OK || data == null) { if (resultCode != RESULT_OK || data == null) {
NativeApp.sendRequestResult(imageRequestId, false, "", 0);
return; return;
} }
if (requestCode == RESULT_LOAD_IMAGE) {
try { try {
Uri selectedImage = data.getData(); Uri selectedImage = data.getData();
if (selectedImage != null) { if (selectedImage != null) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
NativeApp.sendMessage("bgImage_updated", selectedImage.toString()); NativeApp.sendRequestResult(imageRequestId, true, selectedImage.toString(), 0);
} else { } else {
String[] filePathColumn = {MediaStore.Images.Media.DATA}; String[] filePathColumn = {MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null); Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
@ -1147,14 +1150,20 @@ public abstract class NativeActivity extends Activity {
int columnIndex = cursor.getColumnIndex(filePathColumn[0]); int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex); String picturePath = cursor.getString(columnIndex);
cursor.close(); cursor.close();
NativeApp.sendMessage("bgImage_updated", picturePath); NativeApp.sendRequestResult(imageRequestId, true, picturePath, 0);
} }
} }
} else {
NativeApp.sendRequestResult(imageRequestId, false, "", 0);
} }
} catch (Exception e) { } catch (Exception e) {
Log.w(TAG, "Exception receiving image: " + e); Log.w(TAG, "Exception receiving image: " + e);
} }
imageRequestId = -1;
} else if (requestCode == RESULT_OPEN_DOCUMENT) { } else if (requestCode == RESULT_OPEN_DOCUMENT) {
if (resultCode != RESULT_OK || data == null) {
return;
}
Uri selectedFile = data.getData(); Uri selectedFile = data.getData();
if (selectedFile != null) { if (selectedFile != null) {
try { try {
@ -1171,6 +1180,9 @@ public abstract class NativeActivity extends Activity {
NativeApp.sendMessage("browse_fileSelect", selectedFile.toString()); NativeApp.sendMessage("browse_fileSelect", selectedFile.toString());
} }
} else if (requestCode == RESULT_OPEN_DOCUMENT_TREE) { } else if (requestCode == RESULT_OPEN_DOCUMENT_TREE) {
if (resultCode != RESULT_OK || data == null) {
return;
}
Uri selectedDirectoryUri = data.getData(); Uri selectedDirectoryUri = data.getData();
if (selectedDirectoryUri != null) { if (selectedDirectoryUri != null) {
String path = selectedDirectoryUri.toString(); String path = selectedDirectoryUri.toString();
@ -1230,11 +1242,11 @@ public abstract class NativeActivity extends Activity {
return bld; return bld;
} }
// The return value is sent to C++ via seqID. // The return value is sent to C++ via requestID.
public void inputBox(final String seqID, final String title, String defaultText, String defaultAction) { public void inputBox(final int requestId, final String title, String defaultText, String defaultAction) {
// Workaround for issue #13363 to fix Split/Second game start // Workaround for issue #13363 to fix Split/Second game start
if (isVRDevice()) { if (isVRDevice()) {
NativeApp.sendInputBox(seqID, false, defaultText); NativeApp.sendRequestResult(requestId, false, defaultText, 0);
return; return;
} }
@ -1269,14 +1281,14 @@ public abstract class NativeActivity extends Activity {
.setPositiveButton(defaultAction, new DialogInterface.OnClickListener() { .setPositiveButton(defaultAction, new DialogInterface.OnClickListener() {
@Override @Override
public void onClick(DialogInterface d, int which) { public void onClick(DialogInterface d, int which) {
NativeApp.sendInputBox(seqID, true, input.getText().toString()); NativeApp.sendRequestResult(requestId, true, input.getText().toString(), 0);
d.dismiss(); d.dismiss();
} }
}) })
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override @Override
public void onClick(DialogInterface d, int which) { public void onClick(DialogInterface d, int which) {
NativeApp.sendInputBox(seqID, false, ""); NativeApp.sendRequestResult(requestId, false, "", 0);
d.cancel(); d.cancel();
} }
}); });
@ -1284,7 +1296,7 @@ public abstract class NativeActivity extends Activity {
builder.setOnDismissListener(new DialogInterface.OnDismissListener() { builder.setOnDismissListener(new DialogInterface.OnDismissListener() {
@Override @Override
public void onDismiss(DialogInterface d) { public void onDismiss(DialogInterface d) {
NativeApp.sendInputBox(seqID, false, ""); NativeApp.sendRequestResult(requestId, false, "", 0);
updateSystemUiVisibility(); updateSystemUiVisibility();
} }
}); });
@ -1321,8 +1333,10 @@ public abstract class NativeActivity extends Activity {
Log.e(TAG, e.toString()); Log.e(TAG, e.toString());
return false; return false;
} }
} else if (command.equals("bgImage_browse")) { } else if (command.equals("browse_image")) {
try { try {
imageRequestId = Integer.parseInt(params);
Log.i(TAG, "image request ID: " + imageRequestId);
Intent i = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI); Intent i = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, RESULT_LOAD_IMAGE); startActivityForResult(i, RESULT_LOAD_IMAGE);
return true; return true;
@ -1419,13 +1433,13 @@ public abstract class NativeActivity extends Activity {
String title = "Input"; String title = "Input";
String defString = ""; String defString = "";
String[] param = params.split(":@:", 3); String[] param = params.split(":@:", 3);
String seqID = param[0]; int requestID = Integer.parseInt(param[0]);
if (param.length > 1 && param[1].length() > 0) if (param.length > 1 && param[1].length() > 0)
title = param[1]; title = param[1];
if (param.length > 2) if (param.length > 2)
defString = param[2]; defString = param[2];
Log.i(TAG, "Launching inputbox: #" + seqID + " " + title + " " + defString); Log.i(TAG, "Launching inputbox: #" + requestID + " " + title + " " + defString);
inputBox(seqID, title, defString, "OK"); inputBox(requestID, title, defString, "OK");
return true; return true;
} else if (command.equals("vibrate")) { } else if (command.equals("vibrate")) {
int milliseconds = -1; int milliseconds = -1;

View file

@ -51,8 +51,7 @@ public class NativeApp {
public static native void accelerometer(float x, float y, float z); public static native void accelerometer(float x, float y, float z);
public static native void sendMessage(String msg, String arg); public static native void sendMessage(String msg, String arg);
public static native void sendInputBox(String seqID, boolean result, String value); public static native void sendRequestResult(int seqID, boolean result, String value, int iValue);
public static native String queryConfig(String queryName); public static native String queryConfig(String queryName);
public static native int getSelectedCamera(); public static native int getSelectedCamera();