<commit_before>/*
 * Copyright (c) 2015 LingoChamp Inc.
 *
 * 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.liulishuo.filedownloader.demo;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;

import com.liulishuo.filedownloader.BaseDownloadTask;
import com.liulishuo.filedownloader.FileDownloadListener;
import com.liulishuo.filedownloader.FileDownloader;
import com.liulishuo.filedownloader.util.FileDownloadUtils;

import java.io.File;

/**
 * Created by Jacksgong on 12/25/15.
 */
public class PerformanceActivity extends AppCompatActivity {

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

        actionBtn.setTag(true);
        actionBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final boolean toStart = (boolean) actionBtn.getTag();
                if (toStart) {
                    if (start()) {
                        actionBtn.setText("Pause");
                        actionBtn.setTag(false);
                    }
                } else {
                    actionBtn.setText("Start");
                    pause();
                    actionBtn.setTag(true);
                }

            }
        });

        taskCountSb.setMax(Constant.URLS.length);
        taskCountSb.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                taskCountTv.setText(String.valueOf(progress));
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
            }
        });

        deleteAllFileBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int count = 0;
                File file = new File(FileDownloadUtils.getDefaultSaveRootPath());
                do {
                    if (!file.exists()) {
                        break;
                    }

                    if (!file.isDirectory()) {
                        break;
                    }

                    File[] files = file.listFiles();

                    if (files == null) {
                        break;
                    }

                    for (File file1 : files) {
                        count++;
                        file1.delete();
                    }

                } while (false);

                Toast.makeText(PerformanceActivity.this, String.format("Complete delete %d files", count), Toast.LENGTH_LONG).show();

            }
        });

    }

    private long start = 0;

    private boolean start() {
        final int count = Integer.valueOf(taskCountTv.getText().toString());
        if (count <= 0) {
            return false;
        }

        pendingPb.setMax(count);
        connectedPb.setMax(count);
        progressPb.setMax(count);
        errorPb.setMax(count);
        pausedPb.setMax(count);
        completedWidthOldPb.setMax(count);
        completedPb.setMax(count);
        warnPb.setMax(count);

        pendingPb.setProgress(0);
        connectedPb.setProgress(0);
        progressPb.setProgress(0);
        errorPb.setProgress(0);
        pausedPb.setProgress(0);
        completedWidthOldPb.setProgress(0);
        completedPb.setProgress(0);
        warnPb.setProgress(0);

        pendingTv.setText("pending: " + 0);
        connectedTv.setText("connected: " + 0);
        progressTv.setText("progress: " + 0);
        errorTv.setText("error: " + 0);
        pausedTv.setText("paused: " + 0);
        completedWidthOldTv.setText("completed reuse old file: " + 0);
        completedTv.setText("completed width download: " + 0);
        warnTv.setText("warn: " + 0);

        overTaskPb.setMax(count);
        overTaskPb.setProgress(0);

        downloadListener = createLis();
        for (int i = 0; i < count; i++) {
            final String url = Constant.URLS[i];
            FileDownloader.getImpl().create(url)
                    .setListener(downloadListener)
                    .setCallbackProgressTimes(1)
                    .ready();
        }

        isStopTimer = false;
        timeConsumeTv.setTag(0);
        goTimeCount();
        start = System.currentTimeMillis();

        FileDownloader.getImpl().start(downloadListener, serialRbtn.isChecked());

        return true;
    }

    private FileDownloadListener downloadListener;

    private void pause() {
        FileDownloader.getImpl().pause(downloadListener);
        stopTimer();
    }

    private void stopTimer() {
        isStopTimer = true;
        final long consume = System.currentTimeMillis() - start;
        timeConsumeTv.setText(String.valueOf(consume / 1000f));
    }

    private FileDownloadListener createLis() {
        return new FileDownloadListener() {

            @Override
            protected void pending(BaseDownloadTask task, int soFarBytes, int totalBytes) {
                if (task.getListener() != downloadListener) {
                    return;
                }
                pendingPb.setProgress(pendingPb.getProgress() + 1);
                pendingTv.setText("pending: " + pendingPb.getProgress());<commit_msg>demo: Performance Monitor Activity，ondestroy pause task queue<commit_after>/*
 * Copyright (c) 2015 LingoChamp Inc.
 *
 * 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.liulishuo.filedownloader.demo;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;

import com.liulishuo.filedownloader.BaseDownloadTask;
import com.liulishuo.filedownloader.FileDownloadListener;
import com.liulishuo.filedownloader.FileDownloader;
import com.liulishuo.filedownloader.util.FileDownloadUtils;

import java.io.File;

/**
 * Created by Jacksgong on 12/25/15.
 */
public class PerformanceActivity extends AppCompatActivity {

    @Override
    protected void onDestroy() {
        super.onDestroy();
        pause();
    }

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

        actionBtn.setTag(true);
        actionBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final boolean toStart = (boolean) actionBtn.getTag();
                if (toStart) {
                    if (start()) {
                        actionBtn.setText("Pause");
                        actionBtn.setTag(false);
                    }
                } else {
                    actionBtn.setText("Start");
                    pause();
                    actionBtn.setTag(true);
                }

            }
        });

        taskCountSb.setMax(Constant.URLS.length);
        taskCountSb.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                taskCountTv.setText(String.valueOf(progress));
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
            }
        });

        deleteAllFileBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int count = 0;
                File file = new File(FileDownloadUtils.getDefaultSaveRootPath());
                do {
                    if (!file.exists()) {
                        break;
                    }

                    if (!file.isDirectory()) {
                        break;
                    }

                    File[] files = file.listFiles();

                    if (files == null) {
                        break;
                    }

                    for (File file1 : files) {
                        count++;
                        file1.delete();
                    }

                } while (false);

                Toast.makeText(PerformanceActivity.this, String.format("Complete delete %d files", count), Toast.LENGTH_LONG).show();

            }
        });

    }

    private long start = 0;

    private boolean start() {
        final int count = Integer.valueOf(taskCountTv.getText().toString());
        if (count <= 0) {
            return false;
        }

        pendingPb.setMax(count);
        connectedPb.setMax(count);
        progressPb.setMax(count);
        errorPb.setMax(count);
        pausedPb.setMax(count);
        completedWidthOldPb.setMax(count);
        completedPb.setMax(count);
        warnPb.setMax(count);

        pendingPb.setProgress(0);
        connectedPb.setProgress(0);
        progressPb.setProgress(0);
        errorPb.setProgress(0);
        pausedPb.setProgress(0);
        completedWidthOldPb.setProgress(0);
        completedPb.setProgress(0);
        warnPb.setProgress(0);

        pendingTv.setText("pending: " + 0);
        connectedTv.setText("connected: " + 0);
        progressTv.setText("progress: " + 0);
        errorTv.setText("error: " + 0);
        pausedTv.setText("paused: " + 0);
        completedWidthOldTv.setText("completed reuse old file: " + 0);
        completedTv.setText("completed width download: " + 0);
        warnTv.setText("warn: " + 0);

        overTaskPb.setMax(count);
        overTaskPb.setProgress(0);

        downloadListener = createLis();
        for (int i = 0; i < count; i++) {
            final String url = Constant.URLS[i];
            FileDownloader.getImpl().create(url)
                    .setListener(downloadListener)
                    .setCallbackProgressTimes(1)
                    .ready();
        }

        isStopTimer = false;
        timeConsumeTv.setTag(0);
        goTimeCount();
        start = System.currentTimeMillis();

        FileDownloader.getImpl().start(downloadListener, serialRbtn.isChecked());

        return true;
    }

    private FileDownloadListener downloadListener;

    private void pause() {
        FileDownloader.getImpl().pause(downloadListener);
        stopTimer();
    }

    private void stopTimer() {
        isStopTimer = true;
        final long consume = System.currentTimeMillis() - start;
        if (timeConsumeTv != null) {
            timeConsumeTv.setText(String.valueOf(consume / 1000f));
        }
    }

    private FileDownloadListener createLis() {
        return new FileDownloadListener() {

            @Override
            protected void pending(BaseDownloadTask task, int soFarBytes, int totalBytes) {
                if (task.getListener() != downloadListener) {
                    return;
                }
                pendingPb.setProgress(pendingPb.getProgress() + 1);
                pendingTv.setText("pending: " + pendingPb.getProgress());
{'fraction_non_alphanumeric': 0.09838899803536345, 'fraction_numerical': 0.007072691552062868, 'mean_word_length': 1.9397089397089398, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 48, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5834389', 'n_tokens_mistral': 3471, 'n_tokens_neox': 3207, 'n_words': 710}