NativeActivity.java revision a95e4cb62f3642cb190d032dbf7dc40d9ecc6973
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;
9import android.view.InputChannel;
10import android.view.InputConsumer;
11import android.view.SurfaceHolder;
12
13import java.io.File;
14
15/**
16 * Convenience for implementing an activity that will be implemented
17 * purely in native code.  That is, a game (or game-like thing).
18 */
19public class NativeActivity extends Activity implements SurfaceHolder.Callback,
20        InputConsumer.Callback {
21    public static final String META_DATA_LIB_NAME = "android.app.lib_name";
22
23    private int mNativeHandle;
24
25    private native int loadNativeCode(String path);
26    private native void unloadNativeCode(int handle);
27
28    private native void onStartNative(int handle);
29    private native void onResumeNative(int handle);
30    private native void onSaveInstanceStateNative(int handle);
31    private native void onPauseNative(int handle);
32    private native void onStopNative(int handle);
33    private native void onLowMemoryNative(int handle);
34    private native void onWindowFocusChangedNative(int handle, boolean focused);
35    private native void onSurfaceCreatedNative(int handle, SurfaceHolder holder);
36    private native void onSurfaceChangedNative(int handle, SurfaceHolder holder,
37            int format, int width, int height);
38    private native void onSurfaceDestroyedNative(int handle, SurfaceHolder holder);
39    private native void onInputChannelCreatedNative(int handle, InputChannel channel);
40    private native void onInputChannelDestroyedNative(int handle, InputChannel channel);
41
42    @Override
43    protected void onCreate(Bundle savedInstanceState) {
44        String libname = "main";
45        ActivityInfo ai;
46
47        getWindow().takeSurface(this);
48        getWindow().takeInputChannel(this);
49
50        try {
51            ai = getPackageManager().getActivityInfo(
52                    getIntent().getComponent(), PackageManager.GET_META_DATA);
53            if (ai.metaData != null) {
54                String ln = ai.metaData.getString(META_DATA_LIB_NAME);
55                if (ln != null) libname = ln;
56            }
57        } catch (PackageManager.NameNotFoundException e) {
58            throw new RuntimeException("Error getting activity info", e);
59        }
60
61        String path = null;
62
63        if ((ai.applicationInfo.flags&ApplicationInfo.FLAG_HAS_CODE) == 0) {
64            // If the application does not have (Java) code, then no ClassLoader
65            // has been set up for it.  We will need to do our own search for
66            // the native code.
67            path = ai.applicationInfo.dataDir + "/lib/" + System.mapLibraryName(libname);
68            if (!(new File(path)).exists()) {
69                path = null;
70            }
71        }
72
73        if (path == null) {
74            path = ((PathClassLoader)getClassLoader()).findLibrary(libname);
75        }
76
77        if (path == null) {
78            throw new IllegalArgumentException("Unable to find native library: " + libname);
79        }
80
81        mNativeHandle = loadNativeCode(path);
82        if (mNativeHandle == 0) {
83            throw new IllegalArgumentException("Unable to load native library: " + path);
84        }
85        super.onCreate(savedInstanceState);
86    }
87
88    @Override
89    protected void onDestroy() {
90        unloadNativeCode(mNativeHandle);
91        super.onDestroy();
92    }
93
94    @Override
95    protected void onPause() {
96        super.onPause();
97        onPauseNative(mNativeHandle);
98    }
99
100    @Override
101    protected void onResume() {
102        super.onResume();
103        onResumeNative(mNativeHandle);
104    }
105
106    @Override
107    protected void onSaveInstanceState(Bundle outState) {
108        super.onSaveInstanceState(outState);
109        onSaveInstanceStateNative(mNativeHandle);
110    }
111
112    @Override
113    protected void onStart() {
114        super.onStart();
115        onStartNative(mNativeHandle);
116    }
117
118    @Override
119    protected void onStop() {
120        super.onStop();
121        onStopNative(mNativeHandle);
122    }
123
124    @Override
125    public void onLowMemory() {
126        super.onLowMemory();
127        onLowMemoryNative(mNativeHandle);
128    }
129
130    @Override
131    public void onWindowFocusChanged(boolean hasFocus) {
132        super.onWindowFocusChanged(hasFocus);
133        onWindowFocusChangedNative(mNativeHandle, hasFocus);
134    }
135
136    public void surfaceCreated(SurfaceHolder holder) {
137        onSurfaceCreatedNative(mNativeHandle, holder);
138    }
139
140    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
141        onSurfaceChangedNative(mNativeHandle, holder, format, width, height);
142    }
143
144    public void surfaceDestroyed(SurfaceHolder holder) {
145        onSurfaceDestroyedNative(mNativeHandle, holder);
146    }
147
148    public void onInputConsumerCreated(InputConsumer consumer) {
149        onInputChannelCreatedNative(mNativeHandle, consumer.getInputChannel());
150    }
151
152    public void onInputConsumerDestroyed(InputConsumer consumer) {
153        onInputChannelDestroyedNative(mNativeHandle, consumer.getInputChannel());
154    }
155}
156