[Android] #2759: Show a message on failure to load a .so library (by Sylvain)
authorGabriel Jacobo <gabomdq@gmail.com>
Tue, 21 Oct 2014 11:44:35 -0300
changeset 9176 8ebee8e28593
parent 9175 ea006adc8372
child 9177 a3eb1aeb5aac
[Android] #2759: Show a message on failure to load a .so library (by Sylvain)
android-project/src/org/libsdl/app/SDLActivity.java
--- a/android-project/src/org/libsdl/app/SDLActivity.java	Tue Oct 21 11:27:06 2014 -0300
+++ b/android-project/src/org/libsdl/app/SDLActivity.java	Tue Oct 21 11:44:35 2014 -0300
@@ -37,6 +37,7 @@
     // Keep track of the paused state
     public static boolean mIsPaused, mIsSurfaceReady, mHasFocus;
     public static boolean mExitCalledFromJava;
+    public static boolean mBrokenLibraries;
 
     // Main components
     protected static SDLActivity mSingleton;
@@ -52,13 +53,19 @@
     protected static AudioTrack mAudioTrack;
 
     // Load the .so
-    static {
-        System.loadLibrary("SDL2");
-        //System.loadLibrary("SDL2_image");
-        //System.loadLibrary("SDL2_mixer");
-        //System.loadLibrary("SDL2_net");
-        //System.loadLibrary("SDL2_ttf");
-        System.loadLibrary("main");
+    public void loadLibraries() {
+       String AppLibraries[] = {
+         "SDL2",
+         // "SDL2_image",
+         // "SDL2_mixer",
+         // "SDL2_net",
+         // "SDL2_ttf",
+         "main"
+       };
+
+       for (String lib : AppLibraries) {
+          System.loadLibrary(lib);
+       }
     }
     
     /**
@@ -83,6 +90,7 @@
         mSDLThread = null;
         mAudioTrack = null;
         mExitCalledFromJava = false;
+        mBrokenLibraries = false;
         mIsPaused = false;
         mIsSurfaceReady = false;
         mHasFocus = true;
@@ -98,6 +106,35 @@
         // So we can call stuff from static callbacks
         mSingleton = this;
 
+        // Load shared libraries
+        try {
+            loadLibraries();
+        } catch(UnsatisfiedLinkError e) {
+            System.out.println(e.getMessage());
+            mBrokenLibraries = true;
+        } catch(Exception e) {
+            System.out.println(e.getMessage());
+            mBrokenLibraries = true;
+        }
+
+        if (mBrokenLibraries)
+        {
+            AlertDialog.Builder dlgAlert  = new AlertDialog.Builder(this);
+            dlgAlert.setMessage("An error occurred while try to start the application. Please try again and/or reinstall.");
+            dlgAlert.setTitle("SDL Error");
+            dlgAlert.setPositiveButton("EXIT",
+                new DialogInterface.OnClickListener() {
+                    public void onClick(DialogInterface dialog,int id) {
+                        // if this button is clicked, close current activity
+                        SDLActivity.mSingleton.finish();
+                    }
+                });
+           dlgAlert.setCancelable(true);
+           dlgAlert.create().show();
+
+           return;
+        }
+
         // Set up the surface
         mSurface = new SDLSurface(getApplication());
         
@@ -119,6 +156,11 @@
     protected void onPause() {
         Log.v("SDL", "onPause()");
         super.onPause();
+
+        if (SDLActivity.mBrokenLibraries) {
+           return;
+        }
+
         SDLActivity.handlePause();
     }
 
@@ -126,6 +168,11 @@
     protected void onResume() {
         Log.v("SDL", "onResume()");
         super.onResume();
+
+        if (SDLActivity.mBrokenLibraries) {
+           return;
+        }
+
         SDLActivity.handleResume();
     }
 
@@ -135,6 +182,10 @@
         super.onWindowFocusChanged(hasFocus);
         Log.v("SDL", "onWindowFocusChanged(): " + hasFocus);
 
+        if (SDLActivity.mBrokenLibraries) {
+           return;
+        }
+
         SDLActivity.mHasFocus = hasFocus;
         if (hasFocus) {
             SDLActivity.handleResume();
@@ -145,12 +196,25 @@
     public void onLowMemory() {
         Log.v("SDL", "onLowMemory()");
         super.onLowMemory();
+
+        if (SDLActivity.mBrokenLibraries) {
+           return;
+        }
+
         SDLActivity.nativeLowMemory();
     }
 
     @Override
     protected void onDestroy() {
         Log.v("SDL", "onDestroy()");
+
+        if (SDLActivity.mBrokenLibraries) {
+           super.onDestroy();
+           // Reset everything in case the user re opens the app
+           SDLActivity.initialize();
+           return;
+        }
+
         // Send a quit message to the application
         SDLActivity.mExitCalledFromJava = true;
         SDLActivity.nativeQuit();
@@ -174,6 +238,11 @@
 
     @Override
     public boolean dispatchKeyEvent(KeyEvent event) {
+
+        if (SDLActivity.mBrokenLibraries) {
+           return false;
+        }
+
         int keyCode = event.getKeyCode();
         // Ignore certain special keys so they're handled by Android
         if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN ||