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