Skip to content

Commit

Permalink
master: fix for #828
Browse files Browse the repository at this point in the history
  • Loading branch information
Thierry-Dimitri Roy committed May 24, 2016
1 parent c4e9927 commit fe09ccb
Showing 1 changed file with 53 additions and 19 deletions.
72 changes: 53 additions & 19 deletions robotium-solo/src/main/java/com/robotium/solo/ActivityUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@
import android.app.Instrumentation;
import android.app.Instrumentation.ActivityMonitor;
import android.content.IntentFilter;
import android.os.SystemClock;
import android.util.Log;
import android.view.KeyEvent;


/**
* Contains activity related methods. Examples are:
* getCurrentActivity(), getActivityMonitor(), setActivityOrientation(int orientation).
Expand Down Expand Up @@ -145,28 +145,28 @@ private void setupActivityStackListener() {

setRegisterActivities(true);

Runnable runnable = new Runnable() {
public void run() {
while (shouldRegisterActivities()) {
if(activityMonitor != null){
Activity activity = activityMonitor.waitForActivityWithTimeout(2000);

if(activity != null){
if (activitiesStoredInActivityStack.remove(activity.toString())){
removeActivityFromStack(activity);
}
if(!activity.isFinishing()){
addActivityToStack(activity);
}
}
}
activityThread = new RegisterActivitiesThread(this);
activityThread.start();
}


void monitorActivities() {
if(activityMonitor != null){
Activity activity = activityMonitor.waitForActivityWithTimeout(2000L);

if(activity != null){
if (activitiesStoredInActivityStack.remove(activity.toString())){
removeActivityFromStack(activity);
}
if(!activity.isFinishing()){
addActivityToStack(activity);
}
}
};
activityThread = new Thread(runnable, "activityMonitorThread");
activityThread.start();
}
}



/**
* Removes a given activity from the activity stack
*
Expand Down Expand Up @@ -464,4 +464,38 @@ private void finishActivity(Activity activity){
}
}
}

private static final class RegisterActivitiesThread extends Thread {

public static final long REGISTER_ACTIVITY_THREAD_SLEEP_MS = 16L;
private final WeakReference<ActivityUtils> activityUtilsWR;

RegisterActivitiesThread(ActivityUtils activityUtils) {
super("activityMonitorThread");
activityUtilsWR = new WeakReference<ActivityUtils>(activityUtils);
setPriority(Thread.MIN_PRIORITY);
}

@Override
public void run() {
while (shouldMonitor()) {
monitorActivities();
SystemClock.sleep(REGISTER_ACTIVITY_THREAD_SLEEP_MS);
}
}

private boolean shouldMonitor() {
ActivityUtils activityUtils = activityUtilsWR.get();

return activityUtils != null && activityUtils.shouldRegisterActivities();
}

private void monitorActivities() {
ActivityUtils activityUtils = activityUtilsWR.get();
if (activityUtils != null) {
activityUtils.monitorActivities();
}
}
}

}

0 comments on commit fe09ccb

Please sign in to comment.