Skip to content

Instantly share code, notes, and snippets.

@donrokzon
Created March 9, 2018 06:42
Show Gist options
  • Select an option

  • Save donrokzon/3d5bb32e75f721fdd941887c41246b9f to your computer and use it in GitHub Desktop.

Select an option

Save donrokzon/3d5bb32e75f721fdd941887c41246b9f to your computer and use it in GitHub Desktop.
ListFileDownload
/*
* 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.content.Intent;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
import com.liulishuo.filedownloader.BaseDownloadTask;
import com.liulishuo.filedownloader.FileDownloadListener;
import com.liulishuo.filedownloader.FileDownloadQueueSet;
import com.liulishuo.filedownloader.FileDownloader;
import java.util.ArrayList;
import java.util.List;
public class MyDownloader extends AppCompatActivity {
Button button;
String TAG="MyDownloader";
private ProgressBar pendingPb;
private FileDownloadListener downloadListener;
private int totalCounts;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my_downloader);
button=(Button) findViewById(R.id.btn_download);
pendingPb = (ProgressBar) findViewById(R.id.pending_pb);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
onClickMultiSerial();
}
});
button.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View view) {
startActivity(new Intent(MyDownloader.this,MainActivity.class));
return true;
}
});
}
public void onClickMultiSerial() {
final List<BaseDownloadTask> taskList = new ArrayList<>();
final FileDownloadListener serialTarget = createLis();
int i = 0;
for (String url : Constant.URLS2) {
String path=Environment.getExternalStorageDirectory().getAbsolutePath()+"/Download/"+String.valueOf(System.currentTimeMillis()+".jpg");
taskList.add(FileDownloader.getImpl().create(url)
.setPath(path)
.setTag(++i));
}
totalCounts += taskList.size();
new FileDownloadQueueSet(serialTarget)
.setCallbackProgressTimes(100)
.downloadSequentially(taskList)
.start();
}
private FileDownloadListener createLis() {
return new FileDownloadListener() {
@Override
protected void pending(BaseDownloadTask task, int soFarBytes, int totalBytes) {
Log.i(TAG, "pending: ");
}
@Override
protected void connected(BaseDownloadTask task, String etag, boolean isContinue,
int soFarBytes, int totalBytes) {
super.connected(task, etag, isContinue, soFarBytes, totalBytes);
Log.i(TAG, "connected: ");
}
@Override
protected void progress(BaseDownloadTask task, int soFarBytes, int totalBytes) {
Log.i(TAG, "progress: "+String.valueOf(soFarBytes)+" / "+String.valueOf(totalBytes));
pendingPb.setProgress(pendingPb.getProgress() + 1);
}
@Override
protected void blockComplete(BaseDownloadTask task) {
}
@Override
protected void retry(BaseDownloadTask task, Throwable ex, int retryingTimes, int soFarBytes) {
super.retry(task, ex, retryingTimes, soFarBytes);
Log.i(TAG, "retry: "+String.valueOf(retryingTimes));
}
@Override
protected void completed(BaseDownloadTask task) {
Log.i(TAG, "completed: ");
}
@Override
protected void paused(BaseDownloadTask task, int soFarBytes, int totalBytes) {
Log.i(TAG, "paused: ");
}
@Override
protected void error(BaseDownloadTask task, Throwable e) {
Log.d(TAG, "error() returned: " +e.getLocalizedMessage() );
}
@Override
protected void warn(BaseDownloadTask task) {
Log.i(TAG, "warn: ");
}
};
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment