Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TIMOB-23901] Undo changes for running on main thread #8406

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -80,26 +80,20 @@ public static class KrollRuntimeThread extends Thread
private static final String TAG = "KrollRuntimeThread";

private KrollRuntime runtime = null;
private boolean runOnMain;

public KrollRuntimeThread(KrollRuntime runtime, int stackSize, boolean onMainThread)
public KrollRuntimeThread(KrollRuntime runtime, int stackSize)
{
super(null, null, TAG, stackSize);
this.runtime = runtime;
this.runOnMain = onMainThread;
}

public void run()
{
Looper looper;
if (runOnMain) {
looper = Looper.getMainLooper();
} else {
Looper.prepare();
synchronized (this) {
looper = Looper.myLooper();
notifyAll();
}
Looper.prepare();
synchronized (this) {
looper = Looper.myLooper();
notifyAll();
}

// initialize the runtime instance
Expand All @@ -113,40 +107,24 @@ public void run()
// initialize the runtime
runtime.doInit();

if (!runOnMain) {
// start handling messages for this thread
Looper.loop();
}
// start handling messages for this thread
Looper.loop();
}
}

public static void init(Context context, KrollRuntime runtime)
{
KrollAssetHelper.init(context);
// Initialized the runtime if it isn't already initialized
if (runtimeState != State.INITIALIZED) {
boolean onMainThread = runtime.runOnMainThread(context);
int stackSize = runtime.getThreadStackSize(context);
runtime.krollApplication = new WeakReference<KrollApplication>((KrollApplication) context);
runtime.thread = new KrollRuntimeThread(runtime, stackSize, onMainThread);
runtime.thread = new KrollRuntimeThread(runtime, stackSize);
runtime.exceptionHandlers = new HashMap<String, KrollExceptionHandler>();

instance = runtime; // make sure this is set before the runtime thread is started
if (onMainThread) {
runtime.thread.run();
} else {
runtime.thread.start();
}
runtime.thread.start();
}
}

private boolean runOnMainThread(Context context) {
if (context instanceof KrollApplication) {
KrollApplication ka = (KrollApplication) context;
ka.loadAppProperties();
return ka.runOnMainThread();
}
return KrollApplication.DEFAULT_RUN_ON_MAIN_THREAD;
KrollAssetHelper.init(context);
}

public static KrollRuntime getInstance()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,34 +107,29 @@ public static TiMessenger getMainMessenger()
*/
public static TiMessenger getRuntimeMessenger()
{
if (KrollRuntime.getInstance().getKrollApplication().runOnMainThread()) {
return getMainMessenger();
}
return runtimeMessenger;
}

public static void postOnMain(Runnable runnable)
{
TiMessenger messenger = getMainMessenger();
if (messenger == null) {
if (mainMessenger == null) {
Log.w(TAG, "Unable to post runnable on main thread, main messenger is null");

return;
}

messenger.handler.post(runnable);
mainMessenger.handler.post(runnable);
}

public static void postOnRuntime(Runnable runnable)
{
TiMessenger messenger = getRuntimeMessenger();
if (messenger == null) {
if (runtimeMessenger == null) {
Log.w(TAG, "Unable to post runnable on runtime thread, runtime messenger is null");

return;
}

messenger.handler.post(runnable);
runtimeMessenger.handler.post(runnable);
}

/**
Expand All @@ -147,7 +142,7 @@ public static void postOnRuntime(Runnable runnable)
*/
public static Object sendBlockingMainMessage(Message message)
{
return threadLocalMessenger.get().sendBlockingMessage(message, getMainMessenger(), null, -1);
return threadLocalMessenger.get().sendBlockingMessage(message, mainMessenger, null, -1);
}

/**
Expand All @@ -161,7 +156,7 @@ public static Object sendBlockingMainMessage(Message message)
*/
public static Object sendBlockingMainMessage(Message message, Object asyncArg)
{
return threadLocalMessenger.get().sendBlockingMessage(message, getMainMessenger(), asyncArg, -1);
return threadLocalMessenger.get().sendBlockingMessage(message, mainMessenger, asyncArg, -1);
}

/**
Expand All @@ -174,7 +169,7 @@ public static Object sendBlockingMainMessage(Message message, Object asyncArg)
*/
public static Object sendBlockingRuntimeMessage(Message message)
{
return threadLocalMessenger.get().sendBlockingMessage(message, getRuntimeMessenger(), null, -1);
return threadLocalMessenger.get().sendBlockingMessage(message, runtimeMessenger, null, -1);
}

/**
Expand All @@ -188,7 +183,7 @@ public static Object sendBlockingRuntimeMessage(Message message)
*/
public static Object sendBlockingRuntimeMessage(Message message, Object asyncArg)
{
return threadLocalMessenger.get().sendBlockingMessage(message, getRuntimeMessenger(), asyncArg, -1);
return threadLocalMessenger.get().sendBlockingMessage(message, runtimeMessenger, asyncArg, -1);
}

/**
Expand All @@ -204,7 +199,7 @@ public static Object sendBlockingRuntimeMessage(Message message, Object asyncArg
*/
public static Object sendBlockingRuntimeMessage(Message message, Object asyncArg, long maxTimeout)
{
return threadLocalMessenger.get().sendBlockingMessage(message, getRuntimeMessenger(), asyncArg, maxTimeout);
return threadLocalMessenger.get().sendBlockingMessage(message, runtimeMessenger, asyncArg, maxTimeout);
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -647,11 +647,11 @@ public boolean isAnalyticsEnabled()
return getAppInfo().isAnalyticsEnabled();
}

public boolean runOnMainThread()
{
return getAppProperties().getBool("run-on-main-thread", DEFAULT_RUN_ON_MAIN_THREAD);
}

public boolean runOnMainThread()
{
return false;
}
public void setFilterAnalyticsEvents(String[] events)
{
filteredAnalyticsEvents = events;
Expand Down