changed build system for Android from Ant to Gradle
[rocksndiamonds.git] / build-projects / android / app / src / main / java / org / libsdl / app / SDL.java
1 package org.libsdl.app;
2
3 import android.content.Context;
4
5 import java.lang.reflect.*;
6
7 /**
8     SDL library initialization
9 */
10 public class SDL {
11
12     // This function should be called first and sets up the native code
13     // so it can call into the Java classes
14     public static void setupJNI() {
15         SDLActivity.nativeSetupJNI();
16         SDLAudioManager.nativeSetupJNI();
17         SDLControllerManager.nativeSetupJNI();
18     }
19
20     // This function should be called each time the activity is started
21     public static void initialize() {
22         setContext(null);
23
24         SDLActivity.initialize();
25         SDLAudioManager.initialize();
26         SDLControllerManager.initialize();
27     }
28
29     // This function stores the current activity (SDL or not)
30     public static void setContext(Context context) {
31         mContext = context;
32     }
33
34     public static Context getContext() {
35         return mContext;
36     }
37
38     public static void loadLibrary(String libraryName) throws UnsatisfiedLinkError, SecurityException, NullPointerException {
39
40         if (libraryName == null) {
41             throw new NullPointerException("No library name provided.");
42         }
43
44         try {
45             // Let's see if we have ReLinker available in the project.  This is necessary for 
46             // some projects that have huge numbers of local libraries bundled, and thus may 
47             // trip a bug in Android's native library loader which ReLinker works around.  (If
48             // loadLibrary works properly, ReLinker will simply use the normal Android method
49             // internally.)
50             //
51             // To use ReLinker, just add it as a dependency.  For more information, see 
52             // https://github.com/KeepSafe/ReLinker for ReLinker's repository.
53             //
54             Class relinkClass = mContext.getClassLoader().loadClass("com.getkeepsafe.relinker.ReLinker");
55             Class relinkListenerClass = mContext.getClassLoader().loadClass("com.getkeepsafe.relinker.ReLinker$LoadListener");
56             Class contextClass = mContext.getClassLoader().loadClass("android.content.Context");
57             Class stringClass = mContext.getClassLoader().loadClass("java.lang.String");
58
59             // Get a 'force' instance of the ReLinker, so we can ensure libraries are reinstalled if 
60             // they've changed during updates.
61             Method forceMethod = relinkClass.getDeclaredMethod("force");
62             Object relinkInstance = forceMethod.invoke(null);
63             Class relinkInstanceClass = relinkInstance.getClass();
64
65             // Actually load the library!
66             Method loadMethod = relinkInstanceClass.getDeclaredMethod("loadLibrary", contextClass, stringClass, stringClass, relinkListenerClass);
67             loadMethod.invoke(relinkInstance, mContext, libraryName, null, null);
68         }
69         catch (final Throwable e) {
70             // Fall back
71             try {
72                 System.loadLibrary(libraryName);
73             }
74             catch (final UnsatisfiedLinkError ule) {
75                 throw ule;
76             }
77             catch (final SecurityException se) {
78                 throw se;
79             }
80         }        
81     }
82
83     protected static Context mContext;
84 }