You've already forked android_translation_layer
mirror of
https://gitlab.com/android_translation_layer/android_translation_layer.git
synced 2025-10-27 11:48:10 -07:00
MessageQueue: integrate with glib main loop
Adds a special treatment for the main Looper to not block in java code, but instead return to glib managed thread loop. Timeouts in the mainloop are now handled using g_timeout_add_full(). Also defer Activity construction, so that every thing is set up properly when the constructor runs.
This commit is contained in:
@@ -36,7 +36,7 @@ public class Activity extends Context {
|
||||
* @param className class name of activity or null
|
||||
* @return instance of main activity class
|
||||
*/
|
||||
private static Activity createMainActivity(String className) throws ClassNotFoundException, NoSuchMethodException, SecurityException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, IOException {
|
||||
private static Activity createMainActivity(String className, long native_window) throws ClassNotFoundException, NoSuchMethodException, SecurityException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, IOException {
|
||||
if (className == null) {
|
||||
InputStream inStream = ClassLoader.getSystemClassLoader().getResourceAsStream("AndroidManifest.xml");
|
||||
AndroidManifestBlock block = AndroidManifestBlock.load(inStream);
|
||||
@@ -49,11 +49,9 @@ public class Activity extends Context {
|
||||
}
|
||||
Class<? extends Activity> cls = Class.forName(className).asSubclass(Activity.class);
|
||||
Constructor<? extends Activity> constructor = cls.getConstructor();
|
||||
return constructor.newInstance();
|
||||
}
|
||||
|
||||
protected void set_window(long native_window) {
|
||||
window.native_window = native_window;
|
||||
Activity activity = constructor.newInstance();
|
||||
activity.window.native_window = native_window;
|
||||
return activity;
|
||||
}
|
||||
|
||||
public Activity() {
|
||||
@@ -104,12 +102,6 @@ public class Activity extends Context {
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
System.out.println("- onCreate - yay!");
|
||||
|
||||
/* TODO: this probably belongs elsewhere, but this is our entry point for better or worse */
|
||||
Looper looper = Looper.myLooper();
|
||||
if(looper == null) {
|
||||
Looper.prepareMainLooper();
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
@@ -50,7 +50,7 @@ public final class MessageQueue {
|
||||
|
||||
private native static long nativeInit();
|
||||
private native static void nativeDestroy(long ptr);
|
||||
private native static void nativePollOnce(long ptr, int timeoutMillis);
|
||||
private native static boolean nativePollOnce(long ptr, int timeoutMillis);
|
||||
private native static void nativeWake(long ptr);
|
||||
private native static boolean nativeIsIdling(long ptr);
|
||||
|
||||
@@ -134,7 +134,9 @@ public final class MessageQueue {
|
||||
|
||||
// We can assume mPtr != 0 because the loop is obviously still running.
|
||||
// The looper will not call this method after the loop quits.
|
||||
nativePollOnce(mPtr, nextPollTimeoutMillis);
|
||||
if (nativePollOnce(mPtr, nextPollTimeoutMillis)) {
|
||||
return null; // thread is managed by glib, so return instead of blocking
|
||||
}
|
||||
|
||||
synchronized (this) {
|
||||
// Try to retrieve the next message. Return if found.
|
||||
|
||||
Reference in New Issue
Block a user