// Path: AndroidDemos/app/src/main/java/com/zhangtielei/demos/async/programming/common/utils/TextLogUtil.java
// public class TextLogUtil {
//     /**
//      * 在一个TextView上追加一行日志.
//      * @param logTextView
//      * @param log
//      */
//     public static void println(TextView logTextView, CharSequence log) {
//         SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss.SSS");
//         String timestamp = sdf.format(new Date());
// 
//         CharSequence newText = timestamp + ": " + log;
//         CharSequence oldText = logTextView.getText();
//         if (oldText == null || oldText.length() <= 0) {
//             logTextView.setText(newText);
//         }
//         else {
//             logTextView.setText(oldText + "\n" + newText);
//         }
//     }
// }
// 
// Path: AndroidDemos/app/src/main/java/com/zhangtielei/demos/async/programming/queueing/v3/mock/MockAsyncTask.java
// public class MockAsyncTask implements Task<Void> {
//     private static long taskIdCounter;
// 
//     private String taskId;
// 
//     private ExecutorService executorService = Executors.newCachedThreadPool();
//     private Random rand1 = new Random();
//     private Random rand2 = new Random();
// 
//     public MockAsyncTask() {
//         taskId = String.valueOf(++taskIdCounter);
//     }
// 
//     @Override
//     public String getTaskId() {
//         return taskId;
//     }
// 
//     @Override
//     public Observable<Void> start() {
//         return Observable.create(new Observable.OnSubscribe<Void>() {
//             @Override
//             public void call(Subscriber<? super Void> subscriber) {
//                 //任务随机执行0~3秒
//                 try {
//                     TimeUnit.MILLISECONDS.sleep(rand1.nextInt(3000));
//                 } catch (InterruptedException e) {
//                     e.printStackTrace();
//                 }
// 
//                 //模拟失败情况: 以80%的概率失败
//                 Exception error = null;
//                 if (rand2.nextInt(10) < 8) {
//                     error = new RuntimeException("runtime error...");
//                 }
// 
//                 if (error == null) {
//                     //当前异步任务没有返回数据, 不用调用onNext
//                     subscriber.onCompleted();
//                 }
//                 else {
//                     subscriber.onError(error);
//                 }
// 
//             }
//         }).subscribeOn(Schedulers.from(executorService))
//                 .observeOn(AndroidSchedulers.mainThread());
//     }
// 
// 
// }


import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;
import com.zhangtielei.demos.async.programming.R;
import com.zhangtielei.demos.async.programming.common.utils.TextLogUtil;
import com.zhangtielei.demos.async.programming.queueing.v3.mock.MockAsyncTask;
import rx.Observable;
import rx.Subscriber;
import rx.Subscription;
import java.util.ArrayList;
import java.util.List;
/*
 * Copyright (C) 2016 Tielei Zhang (zhangtielei.com).
 *
 * 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 com.zhangtielei.demos.async.programming.queueing.v3;



/**
 * 演示基于RxJava的任务队列的运行情况.
 */
public class TaskQueueDemoActivity extends AppCompatActivity {
    private TextView description;
    private TextView logTextView;

    private TaskQueue taskQueue;

    private List<Subscription> subscriptionList = new ArrayList<Subscription>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_log_display);

        description = (TextView) findViewById(R.id.description);
        logTextView = (TextView) findViewById(R.id.log_display);

        description.setText(R.string.queueing_demo3_description);

        taskQueue = new RxJavaBasedTaskQueue();
        /**
         * 启动5个任务
         */
        for (int i = 0; i < 5; i++) {
// Path: AndroidDemos/app/src/main/java/com/zhangtielei/demos/async/programming/common/utils/TextLogUtil.java
// public class TextLogUtil {
//     /**
//      * 在一个TextView上追加一行日志.
//      * @param logTextView
//      * @param log
//      */
//     public static void println(TextView logTextView, CharSequence log) {
//         SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss.SSS");
//         String timestamp = sdf.format(new Date());
// 
//         CharSequence newText = timestamp + ": " + log;
//         CharSequence oldText = logTextView.getText();
//         if (oldText == null || oldText.length() <= 0) {
//             logTextView.setText(newText);
//         }
//         else {
//             logTextView.setText(oldText + "\n" + newText);
//         }
//     }
// }
// 
// Path: AndroidDemos/app/src/main/java/com/zhangtielei/demos/async/programming/queueing/v3/mock/MockAsyncTask.java
// public class MockAsyncTask implements Task<Void> {
//     private static long taskIdCounter;
// 
//     private String taskId;
// 
//     private ExecutorService executorService = Executors.newCachedThreadPool();
//     private Random rand1 = new Random();
//     private Random rand2 = new Random();
// 
//     public MockAsyncTask() {
//         taskId = String.valueOf(++taskIdCounter);
//     }
// 
//     @Override
//     public String getTaskId() {
//         return taskId;
//     }
// 
//     @Override
//     public Observable<Void> start() {
//         return Observable.create(new Observable.OnSubscribe<Void>() {
//             @Override
//             public void call(Subscriber<? super Void> subscriber) {
//                 //任务随机执行0~3秒
//                 try {
//                     TimeUnit.MILLISECONDS.sleep(rand1.nextInt(3000));
//                 } catch (InterruptedException e) {
//                     e.printStackTrace();
//                 }
// 
//                 //模拟失败情况: 以80%的概率失败
//                 Exception error = null;
//                 if (rand2.nextInt(10) < 8) {
//                     error = new RuntimeException("runtime error...");
//                 }
// 
//                 if (error == null) {
//                     //当前异步任务没有返回数据, 不用调用onNext
//                     subscriber.onCompleted();
//                 }
//                 else {
//                     subscriber.onError(error);
//                 }
// 
//             }
//         }).subscribeOn(Schedulers.from(executorService))
//                 .observeOn(AndroidSchedulers.mainThread());
//     }
// 
// 
// }


// Path: AndroidDemos/app/src/main/java/com/zhangtielei/demos/async/programming/queueing/v3/TaskQueueDemoActivity.java
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;
import com.zhangtielei.demos.async.programming.R;
import com.zhangtielei.demos.async.programming.common.utils.TextLogUtil;
import com.zhangtielei.demos.async.programming.queueing.v3.mock.MockAsyncTask;
import rx.Observable;
import rx.Subscriber;
import rx.Subscription;
import java.util.ArrayList;
import java.util.List;

/*
 * Copyright (C) 2016 Tielei Zhang (zhangtielei.com).
 *
 * 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 com.zhangtielei.demos.async.programming.queueing.v3;



/**
 * 演示基于RxJava的任务队列的运行情况.
 */
public class TaskQueueDemoActivity extends AppCompatActivity {
    private TextView description;
    private TextView logTextView;

    private TaskQueue taskQueue;

    private List<Subscription> subscriptionList = new ArrayList<Subscription>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_log_display);

        description = (TextView) findViewById(R.id.description);
        logTextView = (TextView) findViewById(R.id.log_display);

        description.setText(R.string.queueing_demo3_description);

        taskQueue = new RxJavaBasedTaskQueue();
        /**
         * 启动5个任务
         */
        for (int i = 0; i < 5; i++) {
            final MockAsyncTask task = new MockAsyncTask();