Skip to content

Commit

Permalink
Only run callback with handler in main thread
Browse files Browse the repository at this point in the history
  • Loading branch information
topjohnwu committed Jan 20, 2018
1 parent 91979e3 commit fa5e659
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 13 deletions.
20 changes: 13 additions & 7 deletions superuser/src/main/java/com/topjohnwu/superuser/ShellCallback.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
* <p>
* 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<String> implements Shell.IShellCallback {

private Handler handler;
private Handler handler = null;

public ShellCallback() {
super();
handler = new Handler();
if (Utils.onMainThread())
handler = new Handler();
}

@Override
Expand All @@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
* <p>
* 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<String> implements Shell.IShellCallback {
Expand All @@ -19,18 +19,24 @@ public abstract class ShellCallbackVector extends Vector<String> 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;
}
}
5 changes: 5 additions & 0 deletions superuser/src/main/java/com/topjohnwu/superuser/Utils.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.topjohnwu.superuser;

import android.os.Looper;
import android.util.Log;

import java.security.SecureRandom;
Expand Down Expand Up @@ -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()));
}
}

0 comments on commit fa5e659

Please sign in to comment.