Skip to content

Commit

Permalink
Merge pull request #17672 from hrydgard/android-show-exceptions
Browse files Browse the repository at this point in the history
Android: Show some Java exceptions as they happen
  • Loading branch information
hrydgard authored Jul 6, 2023
2 parents e1060c8 + bc35c33 commit 16efa1d
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 5 deletions.
1 change: 1 addition & 0 deletions Common/System/System.h
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ enum class SystemNotification {
SUSTAINED_PERF_CHANGE,
POLL_CONTROLLERS,
TOGGLE_DEBUG_CONSOLE, // TODO: Kinda weird, just ported forward.
TEST_JAVA_EXCEPTION,
};

std::string System_GetProperty(SystemProperty prop);
Expand Down
15 changes: 10 additions & 5 deletions UI/DevScreens.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -801,19 +801,19 @@ void SystemInfoScreen::CreateTabs() {

internals->Add(new ItemHeader(si->T("Notification tests")));
internals->Add(new Choice(si->T("Error")))->OnClick.Add([&](UI::EventParams &) {
g_OSD.Show(OSDType::MESSAGE_ERROR, si->T("Error"));
g_OSD.Show(OSDType::MESSAGE_ERROR, "Error");
return UI::EVENT_DONE;
});
internals->Add(new Choice(si->T("Warning")))->OnClick.Add([&](UI::EventParams &) {
g_OSD.Show(OSDType::MESSAGE_WARNING, si->T("Warning"), "Some\nAdditional\nDetail");
g_OSD.Show(OSDType::MESSAGE_WARNING, "Warning", "Some\nAdditional\nDetail");
return UI::EVENT_DONE;
});
internals->Add(new Choice(si->T("Info")))->OnClick.Add([&](UI::EventParams &) {
g_OSD.Show(OSDType::MESSAGE_INFO, si->T("Info"));
g_OSD.Show(OSDType::MESSAGE_INFO, "Info");
return UI::EVENT_DONE;
});
internals->Add(new Choice(si->T("Success")))->OnClick.Add([&](UI::EventParams &) {
g_OSD.Show(OSDType::MESSAGE_SUCCESS, si->T("Success"));
g_OSD.Show(OSDType::MESSAGE_SUCCESS, "Success");
return UI::EVENT_DONE;
});
internals->Add(new ItemHeader(si->T("Progress tests")));
Expand All @@ -833,7 +833,12 @@ void SystemInfoScreen::CreateTabs() {
g_OSD.RemoveProgressBar("testprogress", 0.25f);
return UI::EVENT_DONE;
});

#if PPSSPP_PLATFORM(ANDROID)
internals->Add(new Choice(si->T("Exception")))->OnClick.Add([&](UI::EventParams &) {
System_Notify(SystemNotification::TEST_JAVA_EXCEPTION);
return UI::EVENT_DONE;
});
#endif
}

void AddressPromptScreen::CreatePopupContents(UI::ViewGroup *parent) {
Expand Down
5 changes: 5 additions & 0 deletions android/jni/app-android.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1037,6 +1037,9 @@ void System_Notify(SystemNotification notification) {
case SystemNotification::SUSTAINED_PERF_CHANGE:
PushCommand("sustainedPerfMode", "");
break;
case SystemNotification::TEST_JAVA_EXCEPTION:
PushCommand("testException", "This is a test exception");
break;
default:
break;
}
Expand Down Expand Up @@ -1285,6 +1288,8 @@ extern "C" void JNICALL Java_org_ppsspp_ppsspp_NativeApp_sendMessageFromJava(JNI
} else if (msg == "core_powerSaving") {
// Forward.
System_PostUIMessage(msg.c_str(), prm.c_str());
} else if (msg == "exception") {
g_OSD.Show(OSDType::MESSAGE_ERROR, std::string("Java Exception"), prm, 10.0f);
} else {
ERROR_LOG(SYSTEM, "Got unexpected message from Java, ignoring: %s / %s", msg.c_str(), prm.c_str());
}
Expand Down
19 changes: 19 additions & 0 deletions android/src/org/ppsspp/ppsspp/NativeActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,7 @@ public void Initialize() {
additionalStorageDirs = s.toString();
}
catch (Exception e) {
NativeApp.reportException(e, null);
Log.e(TAG, "Failed to get SD storage dirs: " + e.toString());
}

Expand Down Expand Up @@ -1156,6 +1157,7 @@ protected void onActivityResult(int requestCode, int resultCode, Intent data) {
NativeApp.sendRequestResult(imageRequestId, false, "", 0);
}
} catch (Exception e) {
NativeApp.reportException(e, null);
Log.w(TAG, "Exception receiving image: " + e);
}
imageRequestId = -1;
Expand All @@ -1174,6 +1176,8 @@ protected void onActivityResult(int requestCode, int resultCode, Intent data) {
} catch (Exception e) {
Log.w(TAG, "Exception getting permissions for document: " + e.toString());
NativeApp.sendRequestResult(fileRequestId, false, "", 0);
NativeApp.reportException(e, selectedFile.toString());
fileRequestId = -1;
return;
}
// Even if we got an exception getting permissions, try to pass along the file. Maybe this version of Android
Expand All @@ -1185,6 +1189,7 @@ protected void onActivityResult(int requestCode, int resultCode, Intent data) {
} else if (requestCode == RESULT_OPEN_DOCUMENT_TREE) {
if (resultCode != RESULT_OK || data == null) {
NativeApp.sendRequestResult(folderRequestId, false, "", 0);
folderRequestId = -1;
return;
}
Uri selectedDirectoryUri = data.getData();
Expand All @@ -1196,7 +1201,9 @@ protected void onActivityResult(int requestCode, int resultCode, Intent data) {
getContentResolver().takePersistableUriPermission(selectedDirectoryUri, Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
}
} catch (Exception e) {
NativeApp.reportException(e, selectedDirectoryUri.toString());
Log.w(TAG, "Exception getting permissions for document: " + e.toString());
// Intentionally don't return - see below.
}
// Even if we got an exception getting permissions, try to pass along the file. Maybe this version of Android
// doesn't need it. If we can't access it, we'll fail in some other way later.
Expand Down Expand Up @@ -1338,6 +1345,7 @@ public boolean processCommand(String command, String params) {
startActivity(i);
return true;
} catch (Exception e) {
NativeApp.reportException(e, params);
// No browser?
Log.e(TAG, e.toString());
return false;
Expand All @@ -1354,6 +1362,7 @@ public boolean processCommand(String command, String params) {
startActivity(Intent.createChooser(send, "E-mail the app author!"));
return true;
} catch (Exception e) { // For example, android.content.ActivityNotFoundException
NativeApp.reportException(e, params);
Log.e(TAG, e.toString());
return false;
}
Expand All @@ -1365,6 +1374,7 @@ public boolean processCommand(String command, String params) {
startActivityForResult(i, RESULT_LOAD_IMAGE);
return true;
} catch (Exception e) { // For example, android.content.ActivityNotFoundException
NativeApp.reportException(e, params);
imageRequestId = -1;
Log.e(TAG, e.toString());
return false;
Expand All @@ -1382,6 +1392,7 @@ public boolean processCommand(String command, String params) {
startActivityForResult(intent, RESULT_OPEN_DOCUMENT);
// intent.putExtra(DocumentsContract.EXTRA_INITIAL_URI, pickerInitialUri);
} catch (Exception e) {
NativeApp.reportException(e, params);
fileRequestId = -1;
Log.e(TAG, e.toString());
return false;
Expand All @@ -1397,6 +1408,7 @@ public boolean processCommand(String command, String params) {
startActivityForResult(intent, RESULT_OPEN_DOCUMENT_TREE);
return true;
} catch (Exception e) {
NativeApp.reportException(e, params);
folderRequestId = -1;
Log.e(TAG, e.toString());
return false;
Expand All @@ -1411,6 +1423,7 @@ public boolean processCommand(String command, String params) {
startActivity(shareIntent);
return true;
} catch (Exception e) { // For example, android.content.ActivityNotFoundException
NativeApp.reportException(e, params);
Log.e(TAG, e.toString());
return false;
}
Expand Down Expand Up @@ -1558,6 +1571,12 @@ public boolean processCommand(String command, String params) {
// Only keep the screen bright ingame.
window.clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
}
} else if (command.equals("testException")) {
try {
throw new Exception();
} catch (Exception e) {
NativeApp.reportException(e, params);
}
}
return false;
}
Expand Down
19 changes: 19 additions & 0 deletions android/src/org/ppsspp/ppsspp/NativeApp.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,23 @@ public class NativeApp {
public static native void setGpsDataAndroid(long time, float hdop, float latitude, float longitude, float altitude, float speed, float bearing);
public static native void setSatInfoAndroid(short index, short id, short elevation, short azimuth, short snr, short good);
public static native void pushCameraImageAndroid(byte[] image);

// Wrappers
public static void reportException(Exception e, String data) {
String str = e.toString() + "\n" + e.getMessage() + "\n";
if (data != null) {
str += data + "\n";
}
// could also use import android.util.Log; String stackTrace = Log.getStackTraceString(exception);
int count = 0;
for (StackTraceElement ste : e.getStackTrace()) {
str += ste + "\n";
// Only bother with the top of the stack.
if (count > 3) {
break;
}
count++;
}
NativeApp.sendMessageFromJava("exception", str);
}
}

0 comments on commit 16efa1d

Please sign in to comment.