// Path: shelly/src/main/java/xiaofei/library/shelly/runnable/ScheduledRunnable.java
// public class ScheduledRunnable<R> implements Runnable {
// 
//     private Player<R> mPlayer;
// 
//     private Runnable mRunnable;
// 
//     private int mWaiting;
// 
//     public ScheduledRunnable(Player<R> player, Runnable runnable, int waiting) {
//         mPlayer = player;
//         mRunnable = runnable;
//         mWaiting = waiting;
//     }
// 
//     public Runnable getRunnable() {
//         return mRunnable;
//     }
// 
//     public void waitForInput() {
//         int waitingIndex = mWaiting - 1;
//         mPlayer.getInputs().wait(waitingIndex, new Condition<PlayerInputs>() {
//             @Override
//             public boolean satisfy(PlayerInputs o) {
//                 return o.getFinishedNumber().get() == o.getFunctionNumber();
//             }
//         });
//     }
// 
//     public boolean inputSet() {
//         return mPlayer.getInputs().satisfy(mWaiting - 1, new Condition<PlayerInputs>() {
//             @Override
//             public boolean satisfy(PlayerInputs o) {
//                 return o.getFinishedNumber().get() == o.getFunctionNumber();
//             }
//         });
//     }
// 
//     @Override
//     public void run() {
//         waitForInput();
//         mRunnable.run();
//     }
// }
// 
// Path: shelly/src/main/java/xiaofei/library/shelly/util/Config.java
// public class Config {
//     public static final boolean DEBUG = false;
// }


import android.os.Handler;
import android.os.Looper;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import xiaofei.library.shelly.runnable.ScheduledRunnable;
import xiaofei.library.shelly.util.Config;
/**
 *
 * Copyright 2016 Xiaofei
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package xiaofei.library.shelly.scheduler;




/**
 * Created by Xiaofei on 16/5/31.
 */
public class UiThreadScheduler extends Scheduler {

    private static Handler sHandler = new Handler(Looper.getMainLooper());

    //下面这个改变比较重要！！！
    private ExecutorService mExecutor = Executors.newSingleThreadExecutor();

    private void scheduleInternal(Runnable runnable) {
        boolean isMainThread = Looper.getMainLooper() == Looper.myLooper();
        if (isMainThread) {
            if (Config.DEBUG) {
                System.out.println("post ui0 " + Thread.currentThread().getName());
            }
            runnable.run();
        } else {
            if (Config.DEBUG) {
                System.out.println("post ui1 " + Thread.currentThread().getName());
            }
            sHandler.post(runnable);
        }
    }

    @Override
    public void call(Runnable runnable) {
// Path: shelly/src/main/java/xiaofei/library/shelly/runnable/ScheduledRunnable.java
// public class ScheduledRunnable<R> implements Runnable {
// 
//     private Player<R> mPlayer;
// 
//     private Runnable mRunnable;
// 
//     private int mWaiting;
// 
//     public ScheduledRunnable(Player<R> player, Runnable runnable, int waiting) {
//         mPlayer = player;
//         mRunnable = runnable;
//         mWaiting = waiting;
//     }
// 
//     public Runnable getRunnable() {
//         return mRunnable;
//     }
// 
//     public void waitForInput() {
//         int waitingIndex = mWaiting - 1;
//         mPlayer.getInputs().wait(waitingIndex, new Condition<PlayerInputs>() {
//             @Override
//             public boolean satisfy(PlayerInputs o) {
//                 return o.getFinishedNumber().get() == o.getFunctionNumber();
//             }
//         });
//     }
// 
//     public boolean inputSet() {
//         return mPlayer.getInputs().satisfy(mWaiting - 1, new Condition<PlayerInputs>() {
//             @Override
//             public boolean satisfy(PlayerInputs o) {
//                 return o.getFinishedNumber().get() == o.getFunctionNumber();
//             }
//         });
//     }
// 
//     @Override
//     public void run() {
//         waitForInput();
//         mRunnable.run();
//     }
// }
// 
// Path: shelly/src/main/java/xiaofei/library/shelly/util/Config.java
// public class Config {
//     public static final boolean DEBUG = false;
// }


// Path: shelly/src/main/java/xiaofei/library/shelly/scheduler/UiThreadScheduler.java
import android.os.Handler;
import android.os.Looper;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import xiaofei.library.shelly.runnable.ScheduledRunnable;
import xiaofei.library.shelly.util.Config;

/**
 *
 * Copyright 2016 Xiaofei
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package xiaofei.library.shelly.scheduler;




/**
 * Created by Xiaofei on 16/5/31.
 */
public class UiThreadScheduler extends Scheduler {

    private static Handler sHandler = new Handler(Looper.getMainLooper());

    //下面这个改变比较重要！！！
    private ExecutorService mExecutor = Executors.newSingleThreadExecutor();

    private void scheduleInternal(Runnable runnable) {
        boolean isMainThread = Looper.getMainLooper() == Looper.myLooper();
        if (isMainThread) {
            if (Config.DEBUG) {
                System.out.println("post ui0 " + Thread.currentThread().getName());
            }
            runnable.run();
        } else {
            if (Config.DEBUG) {
                System.out.println("post ui1 " + Thread.currentThread().getName());
            }
            sHandler.post(runnable);
        }
    }

    @Override
    public void call(Runnable runnable) {
        if (runnable instanceof ScheduledRunnable) {