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

View file

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

View file

@ -539,8 +539,6 @@ bool System_GetPropertyBool(SystemProperty prop) {
}
void System_Notify(SystemNotification notification) {
switch (notification) {
}
}
std::string Android_GetInputDeviceDebugString() {
@ -1038,34 +1036,28 @@ bool System_MakeRequest(SystemRequestType type, int requestId, const std::string
return true;
}
case SystemRequestType::BROWSE_FOR_IMAGE:
return false;
PushCommand("browse_image", StringFromFormat("%d", requestId));
return true;
default:
return false;
}
}
extern "C" void JNICALL Java_org_ppsspp_ppsspp_NativeApp_sendInputBox(JNIEnv *env, jclass, jstring jseqID, jboolean result, jstring jvalue) {
std::string seqID = GetJavaString(env, jseqID);
extern "C" void JNICALL Java_org_ppsspp_ppsspp_NativeApp_sendRequestResult(JNIEnv *env, jclass, jint jrequestID, jboolean result, jstring jvalue, jint jintValue) {
std::string value = GetJavaString(env, jvalue);
static std::string lastSeqID = "";
if (lastSeqID == seqID) {
static jint lastSeqID = -1;
if (lastSeqID == jrequestID) {
// We send this on dismiss, so twice in many cases.
DEBUG_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());
WARN_LOG(SYSTEM, "Ignoring duplicate sendInputBox");
return;
}
lastSeqID = jrequestID;
if (result) {
g_requestManager.PostSystemSuccess(seq, value.c_str());
g_requestManager.PostSystemSuccess(jrequestID, value.c_str());
} 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") {
sustainedPerfSupported = true;
} 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.
int 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_TREE = 3;
private int imageRequestId = -1;
// Allow for multiple connected gamepads but just consider them the same for now.
// Actually this is not entirely true, see the code.
private ArrayList<InputDeviceState> inputPlayers = new ArrayList<InputDeviceState>();
@ -1130,15 +1132,16 @@ public abstract class NativeActivity extends Activity {
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RESULT_LOAD_IMAGE) {
if (resultCode != RESULT_OK || data == null) {
NativeApp.sendRequestResult(imageRequestId, false, "", 0);
return;
}
if (requestCode == RESULT_LOAD_IMAGE) {
try {
Uri selectedImage = data.getData();
if (selectedImage != null) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
NativeApp.sendMessage("bgImage_updated", selectedImage.toString());
NativeApp.sendRequestResult(imageRequestId, true, selectedImage.toString(), 0);
} else {
String[] filePathColumn = {MediaStore.Images.Media.DATA};
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]);
String picturePath = cursor.getString(columnIndex);
cursor.close();
NativeApp.sendMessage("bgImage_updated", picturePath);
NativeApp.sendRequestResult(imageRequestId, true, picturePath, 0);
}
}
} else {
NativeApp.sendRequestResult(imageRequestId, false, "", 0);
}
} catch (Exception e) {
Log.w(TAG, "Exception receiving image: " + e);
}
imageRequestId = -1;
} else if (requestCode == RESULT_OPEN_DOCUMENT) {
if (resultCode != RESULT_OK || data == null) {
return;
}
Uri selectedFile = data.getData();
if (selectedFile != null) {
try {
@ -1171,6 +1180,9 @@ public abstract class NativeActivity extends Activity {
NativeApp.sendMessage("browse_fileSelect", selectedFile.toString());
}
} else if (requestCode == RESULT_OPEN_DOCUMENT_TREE) {
if (resultCode != RESULT_OK || data == null) {
return;
}
Uri selectedDirectoryUri = data.getData();
if (selectedDirectoryUri != null) {
String path = selectedDirectoryUri.toString();
@ -1230,11 +1242,11 @@ public abstract class NativeActivity extends Activity {
return bld;
}
// The return value is sent to C++ via seqID.
public void inputBox(final String seqID, final String title, String defaultText, String defaultAction) {
// The return value is sent to C++ via requestID.
public void inputBox(final int requestId, final String title, String defaultText, String defaultAction) {
// Workaround for issue #13363 to fix Split/Second game start
if (isVRDevice()) {
NativeApp.sendInputBox(seqID, false, defaultText);
NativeApp.sendRequestResult(requestId, false, defaultText, 0);
return;
}
@ -1269,14 +1281,14 @@ public abstract class NativeActivity extends Activity {
.setPositiveButton(defaultAction, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface d, int which) {
NativeApp.sendInputBox(seqID, true, input.getText().toString());
NativeApp.sendRequestResult(requestId, true, input.getText().toString(), 0);
d.dismiss();
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface d, int which) {
NativeApp.sendInputBox(seqID, false, "");
NativeApp.sendRequestResult(requestId, false, "", 0);
d.cancel();
}
});
@ -1284,7 +1296,7 @@ public abstract class NativeActivity extends Activity {
builder.setOnDismissListener(new DialogInterface.OnDismissListener() {
@Override
public void onDismiss(DialogInterface d) {
NativeApp.sendInputBox(seqID, false, "");
NativeApp.sendRequestResult(requestId, false, "", 0);
updateSystemUiVisibility();
}
});
@ -1321,8 +1333,10 @@ public abstract class NativeActivity extends Activity {
Log.e(TAG, e.toString());
return false;
}
} else if (command.equals("bgImage_browse")) {
} else if (command.equals("browse_image")) {
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);
startActivityForResult(i, RESULT_LOAD_IMAGE);
return true;
@ -1419,13 +1433,13 @@ public abstract class NativeActivity extends Activity {
String title = "Input";
String defString = "";
String[] param = params.split(":@:", 3);
String seqID = param[0];
int requestID = Integer.parseInt(param[0]);
if (param.length > 1 && param[1].length() > 0)
title = param[1];
if (param.length > 2)
defString = param[2];
Log.i(TAG, "Launching inputbox: #" + seqID + " " + title + " " + defString);
inputBox(seqID, title, defString, "OK");
Log.i(TAG, "Launching inputbox: #" + requestID + " " + title + " " + defString);
inputBox(requestID, title, defString, "OK");
return true;
} else if (command.equals("vibrate")) {
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 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 int getSelectedCamera();