First pass at NativeActivity.
This is a rough sketch of the new pure-native API, which you can use through a NativeActivity in your manifest (no Java code in the .apk needed!). Intentionally no docs yet, the API is still being seriously messed with. But it works. Change-Id: I0e916d58a0d159ecaf3689e41834eb8dc681c0c0
This commit is contained in:
122
core/java/android/app/NativeActivity.java
Normal file
122
core/java/android/app/NativeActivity.java
Normal file
@@ -0,0 +1,122 @@
|
||||
package android.app;
|
||||
|
||||
import dalvik.system.PathClassLoader;
|
||||
|
||||
import android.content.pm.ActivityInfo;
|
||||
import android.content.pm.ApplicationInfo;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.os.Bundle;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
/**
|
||||
* Convenience for implementing an activity that will be implemented
|
||||
* purely in native code. That is, a game (or game-like thing).
|
||||
*/
|
||||
public class NativeActivity extends Activity {
|
||||
public static final String META_DATA_LIB_NAME = "android.app.lib_name";
|
||||
|
||||
private int mNativeHandle;
|
||||
|
||||
private native int loadNativeCode(String path);
|
||||
private native void unloadNativeCode(int handle);
|
||||
|
||||
private native void onStartNative(int handle);
|
||||
private native void onResumeNative(int handle);
|
||||
private native void onSaveInstanceStateNative(int handle);
|
||||
private native void onPauseNative(int handle);
|
||||
private native void onStopNative(int handle);
|
||||
private native void onLowMemoryNative(int handle);
|
||||
private native void onWindowFocusChangedNative(int handle, boolean focused);
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
String libname = "main";
|
||||
ActivityInfo ai;
|
||||
|
||||
try {
|
||||
ai = getPackageManager().getActivityInfo(
|
||||
getIntent().getComponent(), PackageManager.GET_META_DATA);
|
||||
if (ai.metaData != null) {
|
||||
String ln = ai.metaData.getString(META_DATA_LIB_NAME);
|
||||
if (ln != null) libname = ln;
|
||||
}
|
||||
} catch (PackageManager.NameNotFoundException e) {
|
||||
throw new RuntimeException("Error getting activity info", e);
|
||||
}
|
||||
|
||||
String path = null;
|
||||
|
||||
if ((ai.applicationInfo.flags&ApplicationInfo.FLAG_HAS_CODE) == 0) {
|
||||
// If the application does not have (Java) code, then no ClassLoader
|
||||
// has been set up for it. We will need to do our own search for
|
||||
// the native code.
|
||||
path = ai.applicationInfo.dataDir + "/lib/" + System.mapLibraryName(libname);
|
||||
if (!(new File(path)).exists()) {
|
||||
path = null;
|
||||
}
|
||||
}
|
||||
|
||||
if (path == null) {
|
||||
path = ((PathClassLoader)getClassLoader()).findLibrary(libname);
|
||||
}
|
||||
|
||||
if (path == null) {
|
||||
throw new IllegalArgumentException("Unable to find native library: " + libname);
|
||||
}
|
||||
|
||||
mNativeHandle = loadNativeCode(path);
|
||||
if (mNativeHandle == 0) {
|
||||
throw new IllegalArgumentException("Unable to load native library: " + path);
|
||||
}
|
||||
super.onCreate(savedInstanceState);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onDestroy() {
|
||||
unloadNativeCode(mNativeHandle);
|
||||
super.onDestroy();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onLowMemory() {
|
||||
super.onLowMemory();
|
||||
onLowMemoryNative(mNativeHandle);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onPause() {
|
||||
super.onPause();
|
||||
onPauseNative(mNativeHandle);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onResume() {
|
||||
super.onResume();
|
||||
onResumeNative(mNativeHandle);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onSaveInstanceState(Bundle outState) {
|
||||
super.onSaveInstanceState(outState);
|
||||
onSaveInstanceStateNative(mNativeHandle);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onStart() {
|
||||
super.onStart();
|
||||
onStartNative(mNativeHandle);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onStop() {
|
||||
super.onStop();
|
||||
onStopNative(mNativeHandle);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onWindowFocusChanged(boolean hasFocus) {
|
||||
super.onWindowFocusChanged(hasFocus);
|
||||
onWindowFocusChangedNative(mNativeHandle, hasFocus);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user