NativeActivity.java revision 69969e48f2bca9339662dddfacff0bbf6374ed7f
1package android.app;
2
3import dalvik.system.PathClassLoader;
4
5import android.content.pm.ActivityInfo;
6import android.content.pm.ApplicationInfo;
7import android.content.pm.PackageManager;
8import android.os.Bundle;
9
10import java.io.File;
11
12/**
13 * Convenience for implementing an activity that will be implemented
14 * purely in native code.  That is, a game (or game-like thing).
15 */
16public class NativeActivity extends Activity {
17    public static final String META_DATA_LIB_NAME = "android.app.lib_name";
18
19    private int mNativeHandle;
20
21    private native int loadNativeCode(String path);
22    private native void unloadNativeCode(int handle);
23
24    private native void onStartNative(int handle);
25    private native void onResumeNative(int handle);
26    private native void onSaveInstanceStateNative(int handle);
27    private native void onPauseNative(int handle);
28    private native void onStopNative(int handle);
29    private native void onLowMemoryNative(int handle);
30    private native void onWindowFocusChangedNative(int handle, boolean focused);
31
32    @Override
33    protected void onCreate(Bundle savedInstanceState) {
34        String libname = "main";
35        ActivityInfo ai;
36
37        try {
38            ai = getPackageManager().getActivityInfo(
39                    getIntent().getComponent(), PackageManager.GET_META_DATA);
40            if (ai.metaData != null) {
41                String ln = ai.metaData.getString(META_DATA_LIB_NAME);
42                if (ln != null) libname = ln;
43            }
44        } catch (PackageManager.NameNotFoundException e) {
45            throw new RuntimeException("Error getting activity info", e);
46        }
47
48        String path = null;
49
50        if ((ai.applicationInfo.flags&ApplicationInfo.FLAG_HAS_CODE) == 0) {
51            // If the application does not have (Java) code, then no ClassLoader
52            // has been set up for it.  We will need to do our own search for
53            // the native code.
54            path = ai.applicationInfo.dataDir + "/lib/" + System.mapLibraryName(libname);
55            if (!(new File(path)).exists()) {
56                path = null;
57            }
58        }
59
60        if (path == null) {
61            path = ((PathClassLoader)getClassLoader()).findLibrary(libname);
62        }
63
64        if (path == null) {
65            throw new IllegalArgumentException("Unable to find native library: " + libname);
66        }
67
68        mNativeHandle = loadNativeCode(path);
69        if (mNativeHandle == 0) {
70            throw new IllegalArgumentException("Unable to load native library: " + path);
71        }
72        super.onCreate(savedInstanceState);
73    }
74
75    @Override
76    protected void onDestroy() {
77        unloadNativeCode(mNativeHandle);
78        super.onDestroy();
79    }
80
81    @Override
82    public void onLowMemory() {
83        super.onLowMemory();
84        onLowMemoryNative(mNativeHandle);
85    }
86
87    @Override
88    protected void onPause() {
89        super.onPause();
90        onPauseNative(mNativeHandle);
91    }
92
93    @Override
94    protected void onResume() {
95        super.onResume();
96        onResumeNative(mNativeHandle);
97    }
98
99    @Override
100    protected void onSaveInstanceState(Bundle outState) {
101        super.onSaveInstanceState(outState);
102        onSaveInstanceStateNative(mNativeHandle);
103    }
104
105    @Override
106    protected void onStart() {
107        super.onStart();
108        onStartNative(mNativeHandle);
109    }
110
111    @Override
112    protected void onStop() {
113        super.onStop();
114        onStopNative(mNativeHandle);
115    }
116
117    @Override
118    public void onWindowFocusChanged(boolean hasFocus) {
119        super.onWindowFocusChanged(hasFocus);
120        onWindowFocusChangedNative(mNativeHandle, hasFocus);
121    }
122}
123