diff --git a/superuser/src/main/java/com/topjohnwu/superuser/ShellCallback.java b/superuser/src/main/java/com/topjohnwu/superuser/ShellCallback.java
index b8888b20..d2bbc8b7 100644
--- a/superuser/src/main/java/com/topjohnwu/superuser/ShellCallback.java
+++ b/superuser/src/main/java/com/topjohnwu/superuser/ShellCallback.java
@@ -8,18 +8,19 @@
* An {@link AbstractList} only used as an abstract container to call {@link #onShellOutput(String)}
* when a new output is added to the list.
*
- * The method {@link #onShellOutput(String)} will be called in the thread where the
- * {@link ShellCallbackVector} is constructed by using {@link Handler}. If you need to update
- * the UI, please construct the list in the main thread.
+ * The method {@link #onShellOutput(String)} will be called with a {@link Handler} if the
+ * {@link ShellCallback} is constructed in the main thread (UI thread). If you need to update
+ * the UI, please construct the new instance in the main thread.
*/
public abstract class ShellCallback extends AbstractList implements Shell.IShellCallback {
- private Handler handler;
+ private Handler handler = null;
public ShellCallback() {
super();
- handler = new Handler();
+ if (Utils.onMainThread())
+ handler = new Handler();
}
@Override
@@ -29,12 +30,17 @@ public final int size() {
@Override
public synchronized boolean add(final String s) {
- handler.post(new Runnable() {
+ Runnable run = new Runnable() {
@Override
public void run() {
ShellCallback.this.onShellOutput(s);
}
- });
+ };
+ if (handler != null) {
+ handler.post(run);
+ } else {
+ run.run();
+ }
return true;
}
diff --git a/superuser/src/main/java/com/topjohnwu/superuser/ShellCallbackVector.java b/superuser/src/main/java/com/topjohnwu/superuser/ShellCallbackVector.java
index 1fa61382..855b0f98 100644
--- a/superuser/src/main/java/com/topjohnwu/superuser/ShellCallbackVector.java
+++ b/superuser/src/main/java/com/topjohnwu/superuser/ShellCallbackVector.java
@@ -8,9 +8,9 @@
* A {@link Vector} to store output of {@link Shell} and call {@link #onShellOutput(String)} when
* a new output is added to the list.
*
- * The method {@link #onShellOutput(String)} will be called in the thread where the
- * {@link ShellCallbackVector} is constructed by using {@link Handler}. If you need to update
- * the UI, please construct the list in the main thread.
+ * The method {@link #onShellOutput(String)} will be called with a {@link Handler} if the
+ * {@link ShellCallbackVector} is constructed in the main thread (UI thread). If you need to update
+ * the UI, please construct the new instance in the main thread.
*/
public abstract class ShellCallbackVector extends Vector implements Shell.IShellCallback {
@@ -19,18 +19,24 @@ public abstract class ShellCallbackVector extends Vector implements Shel
public ShellCallbackVector() {
super();
- handler = new Handler();
+ if (Utils.onMainThread())
+ handler = new Handler();
}
@Override
public boolean add(final String s) {
boolean ret = super.add(s);
- handler.post(new Runnable() {
+ Runnable run = new Runnable() {
@Override
public void run() {
ShellCallbackVector.this.onShellOutput(s);
}
- });
+ };
+ if (handler != null) {
+ handler.post(run);
+ } else {
+ run.run();
+ }
return ret;
}
}
diff --git a/superuser/src/main/java/com/topjohnwu/superuser/Utils.java b/superuser/src/main/java/com/topjohnwu/superuser/Utils.java
index acb20f29..9eb80d80 100644
--- a/superuser/src/main/java/com/topjohnwu/superuser/Utils.java
+++ b/superuser/src/main/java/com/topjohnwu/superuser/Utils.java
@@ -1,5 +1,6 @@
package com.topjohnwu.superuser;
+import android.os.Looper;
import android.util.Log;
import java.security.SecureRandom;
@@ -37,4 +38,8 @@ static void stackTrace(Throwable t) {
if (hasFlag(Shell.FLAG_VERBOSE_LOGGING))
t.printStackTrace();
}
+
+ static boolean onMainThread() {
+ return ((Looper.myLooper() != null) && (Looper.myLooper() == Looper.getMainLooper()));
+ }
}