app/internal/window: [Android] move main thread dispatching to Go

Signed-off-by: Elias Naur <mail@eliasnaur.com>
This commit is contained in:
Elias Naur
2020-05-17 15:46:17 +02:00
parent bb4a02babc
commit 503534e84e
2 changed files with 41 additions and 55 deletions
+19 -31
View File
@@ -145,14 +145,6 @@ public final class GioView extends SurfaceView implements Choreographer.FrameCal
}); });
} }
void postFrameCallbackOnMainThread() {
handler.post(new Runnable() {
@Override public void run() {
postFrameCallback();
}
});
}
@Override protected boolean fitSystemWindows(Rect insets) { @Override protected boolean fitSystemWindows(Rect insets) {
onWindowInsets(nhandle, insets.top, insets.right, insets.bottom, insets.left); onWindowInsets(nhandle, insets.top, insets.right, insets.bottom, insets.left);
return true; return true;
@@ -201,7 +193,7 @@ public final class GioView extends SurfaceView implements Choreographer.FrameCal
return onBack(nhandle); return onBack(nhandle);
} }
protected void wakeupMainThread() { void wakeupMainThread() {
handler.post(new Runnable() { handler.post(new Runnable() {
@Override public void run() { @Override public void run() {
scheduleMainFuncs(); scheduleMainFuncs();
@@ -209,7 +201,7 @@ public final class GioView extends SurfaceView implements Choreographer.FrameCal
}); });
} }
protected void registerFragment(String del) { void registerFragment(String del) {
final Class cls; final Class cls;
try { try {
cls = getContext().getClassLoader().loadClass(del); cls = getContext().getClassLoader().loadClass(del);
@@ -217,33 +209,29 @@ public final class GioView extends SurfaceView implements Choreographer.FrameCal
throw new RuntimeException("RegisterFragment: fragment class not found: " + e.getMessage()); throw new RuntimeException("RegisterFragment: fragment class not found: " + e.getMessage());
} }
handler.post(new Runnable() { final Fragment frag;
public void run() { try {
final Fragment frag; frag = (Fragment)cls.newInstance();
try { } catch (IllegalAccessException | InstantiationException | ExceptionInInitializerError | SecurityException | ClassCastException e) {
frag = (Fragment)cls.newInstance(); throw new RuntimeException("RegisterFragment: error instantiating fragment: " + e.getMessage());
} catch (IllegalAccessException | InstantiationException | ExceptionInInitializerError | SecurityException | ClassCastException e) { }
throw new RuntimeException("RegisterFragment: error instantiating fragment: " + e.getMessage()); final FragmentManager fm;
} try {
final FragmentManager fm; fm = ((Activity)getContext()).getFragmentManager();
try { } catch (ClassCastException e) {
fm = ((Activity)getContext()).getFragmentManager(); throw new RuntimeException("RegisterFragment: cannot get fragment manager from View Context: " + e.getMessage());
} catch (ClassCastException e) { }
throw new RuntimeException("RegisterFragment: cannot get fragment manager from View Context: " + e.getMessage()); FragmentTransaction ft = fm.beginTransaction();
} ft.add(frag, del);
FragmentTransaction ft = fm.beginTransaction(); ft.commitNow();
ft.add(frag, del);
ft.commitNow();
}
});
} }
protected void writeClipboard(String s) { void writeClipboard(String s) {
ClipboardManager m = (ClipboardManager)getContext().getSystemService(Context.CLIPBOARD_SERVICE); ClipboardManager m = (ClipboardManager)getContext().getSystemService(Context.CLIPBOARD_SERVICE);
m.setPrimaryClip(ClipData.newPlainText(null, s)); m.setPrimaryClip(ClipData.newPlainText(null, s));
} }
protected String readClipboard() { String readClipboard() {
ClipboardManager m = (ClipboardManager)getContext().getSystemService(Context.CLIPBOARD_SERVICE); ClipboardManager m = (ClipboardManager)getContext().getSystemService(Context.CLIPBOARD_SERVICE);
ClipData c = m.getPrimaryClip(); ClipData c = m.getPrimaryClip();
if (c == null || c.getItemCount() < 1) { if (c == null || c.getItemCount() < 1) {
+22 -24
View File
@@ -75,16 +75,15 @@ type window struct {
animating bool animating bool
// Cached Java methods. // Cached Java methods.
mgetDensity C.jmethodID mgetDensity C.jmethodID
mgetFontScale C.jmethodID mgetFontScale C.jmethodID
mshowTextInput C.jmethodID mshowTextInput C.jmethodID
mhideTextInput C.jmethodID mhideTextInput C.jmethodID
mpostFrameCallback C.jmethodID mpostFrameCallback C.jmethodID
mpostFrameCallbackOnMainThread C.jmethodID mRegisterFragment C.jmethodID
mRegisterFragment C.jmethodID mwakeupMainThread C.jmethodID
mwakeupMainThread C.jmethodID mwriteClipboard C.jmethodID
mwriteClipboard C.jmethodID mreadClipboard C.jmethodID
mreadClipboard C.jmethodID
} }
type jvalue uint64 // The largest JNI type fits in 64 bits. type jvalue uint64 // The largest JNI type fits in 64 bits.
@@ -181,17 +180,16 @@ func GetDataDir() string {
func Java_org_gioui_GioView_onCreateView(env *C.JNIEnv, class C.jclass, view C.jobject) C.jlong { func Java_org_gioui_GioView_onCreateView(env *C.JNIEnv, class C.jclass, view C.jobject) C.jlong {
view = C.gio_jni_NewGlobalRef(env, view) view = C.gio_jni_NewGlobalRef(env, view)
w := &window{ w := &window{
view: view, view: view,
mgetDensity: getMethodID(env, class, "getDensity", "()I"), mgetDensity: getMethodID(env, class, "getDensity", "()I"),
mgetFontScale: getMethodID(env, class, "getFontScale", "()F"), mgetFontScale: getMethodID(env, class, "getFontScale", "()F"),
mshowTextInput: getMethodID(env, class, "showTextInput", "()V"), mshowTextInput: getMethodID(env, class, "showTextInput", "()V"),
mhideTextInput: getMethodID(env, class, "hideTextInput", "()V"), mhideTextInput: getMethodID(env, class, "hideTextInput", "()V"),
mpostFrameCallback: getMethodID(env, class, "postFrameCallback", "()V"), mpostFrameCallback: getMethodID(env, class, "postFrameCallback", "()V"),
mpostFrameCallbackOnMainThread: getMethodID(env, class, "postFrameCallbackOnMainThread", "()V"), mRegisterFragment: getMethodID(env, class, "registerFragment", "(Ljava/lang/String;)V"),
mRegisterFragment: getMethodID(env, class, "registerFragment", "(Ljava/lang/String;)V"), mwakeupMainThread: getMethodID(env, class, "wakeupMainThread", "()V"),
mwakeupMainThread: getMethodID(env, class, "wakeupMainThread", "()V"), mwriteClipboard: getMethodID(env, class, "writeClipboard", "(Ljava/lang/String;)V"),
mwriteClipboard: getMethodID(env, class, "writeClipboard", "(Ljava/lang/String;)V"), mreadClipboard: getMethodID(env, class, "readClipboard", "()Ljava/lang/String;"),
mreadClipboard: getMethodID(env, class, "readClipboard", "()Ljava/lang/String;"),
} }
wopts := <-mainWindow.out wopts := <-mainWindow.out
w.callbacks = wopts.window w.callbacks = wopts.window
@@ -370,8 +368,8 @@ func (w *window) SetAnimating(anim bool) {
w.animating = anim w.animating = anim
w.mu.Unlock() w.mu.Unlock()
if anim { if anim {
runInJVM(javaVM(), func(env *C.JNIEnv) { w.runOnMain(func(env *C.JNIEnv) {
callVoidMethod(env, w.view, w.mpostFrameCallbackOnMainThread) callVoidMethod(env, w.view, w.mpostFrameCallback)
}) })
} }
} }
@@ -532,7 +530,7 @@ func javaString(env *C.JNIEnv, str string) C.jstring {
} }
func (w *window) RegisterFragment(del string) { func (w *window) RegisterFragment(del string) {
runInJVM(javaVM(), func(env *C.JNIEnv) { w.runOnMain(func(env *C.JNIEnv) {
jstr := javaString(env, del) jstr := javaString(env, del)
callVoidMethod(env, w.view, w.mRegisterFragment, jvalue(jstr)) callVoidMethod(env, w.view, w.mRegisterFragment, jvalue(jstr))
}) })