// 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);
//         }
//     }
// }


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 java.util.Random;
import java.util.concurrent.TimeUnit;
/*
 * 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.v1;



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

    private TaskQueue taskQueue;

    private static long taskIdCounter;
    private Random rand1 = new Random();
    private Random rand2 = new Random();

    @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_demo1_description);

        taskQueue = new TSQBasedTaskQueue();
        taskQueue.setListener(new TaskQueue.TaskQueueListener() {
            @Override
            public void taskComplete(Task task) {
// 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/v1/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 java.util.Random;
import java.util.concurrent.TimeUnit;

/*
 * 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.v1;



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

    private TaskQueue taskQueue;

    private static long taskIdCounter;
    private Random rand1 = new Random();
    private Random rand2 = new Random();

    @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_demo1_description);

        taskQueue = new TSQBasedTaskQueue();
        taskQueue.setListener(new TaskQueue.TaskQueueListener() {
            @Override
            public void taskComplete(Task task) {
                TextLogUtil.println(logTextView, "Task (" + task.getTaskId() + ") complete");