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