Subclassing NativeActivity makes two things possible. Firstly, we can implement an InputConnection to offer good support for IMEs, necessary for good keyboard support. Secondly, we can use it to overlay WebViews onto the NativeActivity. But to sublcass NativeActivity, we need to compile Java. To keep the toolchain go gettable, this is done with go generate. While here, check the exception after FindClass. Apparently it can throw an exception. Updates golang/go#9361. Updates golang/go#10247. Change-Id: I672545997f0c9a7580f06988a273c03404772247 Reviewed-on: https://go-review.googlesource.com/11980 Reviewed-by: Hyang-Ah Hana Kim <hyangah@gmail.com>
52 lines
1.4 KiB
Java
52 lines
1.4 KiB
Java
package org.golang.app;
|
|
|
|
import android.app.Activity;
|
|
import android.app.NativeActivity;
|
|
import android.content.Context;
|
|
import android.content.pm.ActivityInfo;
|
|
import android.content.pm.PackageManager;
|
|
import android.os.Bundle;
|
|
import android.util.Log;
|
|
|
|
public class GoNativeActivity extends NativeActivity {
|
|
private static GoNativeActivity goNativeActivity;
|
|
|
|
public GoNativeActivity() {
|
|
super();
|
|
goNativeActivity = this;
|
|
}
|
|
|
|
String getTmpdir() {
|
|
return getCacheDir().getAbsolutePath();
|
|
}
|
|
|
|
private void load() {
|
|
// Interestingly, NativeActivity uses a different method
|
|
// to find native code to execute, avoiding
|
|
// System.loadLibrary. The result is Java methods
|
|
// implemented in C with JNIEXPORT (and JNI_OnLoad) are not
|
|
// available unless an explicit call to System.loadLibrary
|
|
// is done. So we do it here, borrowing the name of the
|
|
// library from the same AndroidManifest.xml metadata used
|
|
// by NativeActivity.
|
|
try {
|
|
ActivityInfo ai = getPackageManager().getActivityInfo(
|
|
getIntent().getComponent(), PackageManager.GET_META_DATA);
|
|
if (ai.metaData == null) {
|
|
Log.e("Go", "loadLibrary: no manifest metadata found");
|
|
return;
|
|
}
|
|
String libName = ai.metaData.getString("android.app.lib_name");
|
|
System.loadLibrary(libName);
|
|
} catch (Exception e) {
|
|
Log.e("Go", "loadLibrary failed", e);
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public void onCreate(Bundle savedInstanceState) {
|
|
load();
|
|
super.onCreate(savedInstanceState);
|
|
}
|
|
}
|