Handle failure to load hidapi gracefully
authorSam Lantinga <slouken@libsdl.org>
Mon, 22 Oct 2018 14:55:45 -0700
changeset 1234794de324eb7da
parent 12346 b4c98c84a0b2
child 12348 09d04d83c3c1
Handle failure to load hidapi gracefully
android-project/app/src/main/java/org/libsdl/app/HIDDeviceManager.java
android-project/app/src/main/java/org/libsdl/app/SDL.java
     1.1 --- a/android-project/app/src/main/java/org/libsdl/app/HIDDeviceManager.java	Mon Oct 22 14:55:42 2018 -0700
     1.2 +++ b/android-project/app/src/main/java/org/libsdl/app/HIDDeviceManager.java	Mon Oct 22 14:55:45 2018 -0700
     1.3 @@ -1,5 +1,7 @@
     1.4  package org.libsdl.app;
     1.5  
     1.6 +import android.app.Activity;
     1.7 +import android.app.AlertDialog;
     1.8  import android.app.PendingIntent;
     1.9  import android.bluetooth.BluetoothAdapter;
    1.10  import android.bluetooth.BluetoothDevice;
    1.11 @@ -8,6 +10,7 @@
    1.12  import android.util.Log;
    1.13  import android.content.BroadcastReceiver;
    1.14  import android.content.Context;
    1.15 +import android.content.DialogInterface;
    1.16  import android.content.Intent;
    1.17  import android.content.IntentFilter;
    1.18  import android.content.SharedPreferences;
    1.19 @@ -98,17 +101,39 @@
    1.20          }
    1.21      };
    1.22  
    1.23 -    private HIDDeviceManager(Context context) {
    1.24 +    private HIDDeviceManager(final Context context) {
    1.25          mContext = context;
    1.26  
    1.27          // Make sure we have the HIDAPI library loaded with the native functions
    1.28          try {
    1.29              SDL.loadLibrary("hidapi");
    1.30 -        } catch (Exception e) {
    1.31 +        } catch (Throwable e) {
    1.32              Log.w(TAG, "Couldn't load hidapi: " + e.toString());
    1.33 +
    1.34 +            AlertDialog.Builder builder = new AlertDialog.Builder(context);
    1.35 +            builder.setCancelable(false);
    1.36 +            builder.setTitle("SDL HIDAPI Error");
    1.37 +            builder.setMessage("Please report the following error to the SDL maintainers: " + e.getMessage());
    1.38 +            builder.setNegativeButton("Quit", new DialogInterface.OnClickListener() {
    1.39 +                @Override
    1.40 +                public void onClick(DialogInterface dialog, int which) {
    1.41 +                    try {
    1.42 +                        // If our context is an activity, exit rather than crashing when we can't
    1.43 +                        // call our native functions.
    1.44 +                        Activity activity = (Activity)context;
    1.45 +        
    1.46 +                        activity.finish();
    1.47 +                    }
    1.48 +                    catch (ClassCastException cce) {
    1.49 +                        // Context wasn't an activity, there's nothing we can do.  Give up and return.
    1.50 +                    }
    1.51 +                }
    1.52 +            });
    1.53 +            builder.show();
    1.54 +
    1.55              return;
    1.56          }
    1.57 -
    1.58 +        
    1.59          HIDDeviceRegisterCallback();
    1.60  
    1.61          mSharedPreferences = mContext.getSharedPreferences("hidapi", Context.MODE_PRIVATE);
     2.1 --- a/android-project/app/src/main/java/org/libsdl/app/SDL.java	Mon Oct 22 14:55:42 2018 -0700
     2.2 +++ b/android-project/app/src/main/java/org/libsdl/app/SDL.java	Mon Oct 22 14:55:45 2018 -0700
     2.3 @@ -66,7 +66,7 @@
     2.4              Method loadMethod = relinkInstanceClass.getDeclaredMethod("loadLibrary", contextClass, stringClass, stringClass, relinkListenerClass);
     2.5              loadMethod.invoke(relinkInstance, mContext, libraryName, null, null);
     2.6          }
     2.7 -        catch (final Exception e) {
     2.8 +        catch (final Throwable e) {
     2.9              // Fall back
    2.10              try {
    2.11                  System.loadLibrary(libraryName);