Handle failure to load hidapi gracefully
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
diff --git a/android-project/app/src/main/java/org/libsdl/app/HIDDeviceManager.java b/android-project/app/src/main/java/org/libsdl/app/HIDDeviceManager.java
index 761ecca..db9400f 100644
--- a/android-project/app/src/main/java/org/libsdl/app/HIDDeviceManager.java
+++ b/android-project/app/src/main/java/org/libsdl/app/HIDDeviceManager.java
@@ -1,5 +1,7 @@
package org.libsdl.app;
+import android.app.Activity;
+import android.app.AlertDialog;
import android.app.PendingIntent;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
@@ -8,6 +10,7 @@ import android.bluetooth.BluetoothProfile;
import android.util.Log;
import android.content.BroadcastReceiver;
import android.content.Context;
+import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
@@ -98,17 +101,39 @@ public class HIDDeviceManager {
}
};
- private HIDDeviceManager(Context context) {
+ private HIDDeviceManager(final Context context) {
mContext = context;
// Make sure we have the HIDAPI library loaded with the native functions
try {
SDL.loadLibrary("hidapi");
- } catch (Exception e) {
+ } catch (Throwable e) {
Log.w(TAG, "Couldn't load hidapi: " + e.toString());
+
+ AlertDialog.Builder builder = new AlertDialog.Builder(context);
+ builder.setCancelable(false);
+ builder.setTitle("SDL HIDAPI Error");
+ builder.setMessage("Please report the following error to the SDL maintainers: " + e.getMessage());
+ builder.setNegativeButton("Quit", new DialogInterface.OnClickListener() {
+ @Override
+ public void onClick(DialogInterface dialog, int which) {
+ try {
+ // If our context is an activity, exit rather than crashing when we can't
+ // call our native functions.
+ Activity activity = (Activity)context;
+
+ activity.finish();
+ }
+ catch (ClassCastException cce) {
+ // Context wasn't an activity, there's nothing we can do. Give up and return.
+ }
+ }
+ });
+ builder.show();
+
return;
}
-
+
HIDDeviceRegisterCallback();
mSharedPreferences = mContext.getSharedPreferences("hidapi", Context.MODE_PRIVATE);
diff --git a/android-project/app/src/main/java/org/libsdl/app/SDL.java b/android-project/app/src/main/java/org/libsdl/app/SDL.java
index 58597c6..fb7f731 100644
--- a/android-project/app/src/main/java/org/libsdl/app/SDL.java
+++ b/android-project/app/src/main/java/org/libsdl/app/SDL.java
@@ -66,7 +66,7 @@ public class SDL {
Method loadMethod = relinkInstanceClass.getDeclaredMethod("loadLibrary", contextClass, stringClass, stringClass, relinkListenerClass);
loadMethod.invoke(relinkInstance, mContext, libraryName, null, null);
}
- catch (final Exception e) {
+ catch (final Throwable e) {
// Fall back
try {
System.loadLibrary(libraryName);