提交项目

This commit is contained in:
huangzhuanghua
2016-04-25 11:18:59 +08:00
commit 3f29f7b39a
660 changed files with 68059 additions and 0 deletions

View File

@ -0,0 +1,187 @@
package com.gh.download;
import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map.Entry;
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import com.gh.common.constant.Constants;
import com.gh.common.util.FileUtils;
import com.gh.common.util.Trace;
public class DownloadTask implements DownloadListener {
private DownloadEntry entry;
private Handler handler;
private Context context;
private DownloadThread downloadThread;
private double currPercent;
private long totalSize;
private long currSize;
// 计算下载速度
private long lastTime;
private long intervalDownloadedSize;
public DownloadTask(Handler handler, final DownloadEntry entry,
Context context) {
this.entry = entry;
this.handler = handler;
this.context = context;
DownloadEntry historyEntry = DownloadDao.getInstance(context).get(
entry.getUrl());
if (historyEntry != null && historyEntry.getSize() != 0) {
// 历史下载任务,初始化数据
this.totalSize = entry.getSize();
this.currPercent = (int) (entry.getProgress() * 100l / totalSize);
Trace.getInstance().debug(DownloadTask.class.getSimpleName(),
"DownloadTask==>" + "历史下载任务,初始化数据");
} else {
// 第一次下载
this.currSize = 0;
this.currPercent = 0;
}
}
public void start() {
// 从下载历史里获取过去下载的进度信息
DownloadEntry downloadEntry = null;
downloadEntry = DownloadDao.getInstance(context).get(entry.getUrl());
if (downloadEntry != null) {
entry = downloadEntry;
entry.setStatus(DownloadStatus.downloading);
Trace.getInstance().debug(
DownloadTask.class.getSimpleName(),
"start==>" + "load from history:" + entry.getUrl() + "--"
+ entry.getProgress() + "," + entry.getSize());
currSize = entry.getProgress();
currPercent = entry.getPercent();
} else {
Trace.getInstance().debug(DownloadTask.class.getSimpleName(),
"start==>" + "First log into history!");
}
downloadThread = new DownloadThread(context, this.entry, this);
downloadThread.setPriority(Thread.MAX_PRIORITY);
downloadThread.start();
}
@Override
public synchronized void onProgressChanged(int len) {
currSize += len;
double percent = 0;
if (entry.getSize() != 0) {
percent = currSize * 100.0 / entry.getSize();
DecimalFormat df = new DecimalFormat("#.0");
percent = Double.parseDouble(df.format(percent));
}
entry.setProgress(currSize);
entry.setPercent(percent);
if (percent > currPercent) {
currPercent = percent;
Message msg = handler.obtainMessage();
handler.removeMessages(0);
msg.what = 0;
msg.obj = entry;
handler.sendMessage(msg);
DownloadDao.getInstance(context).newOrUpdate(entry);
}
// 计算下载速度
if (System.currentTimeMillis() - lastTime > Constants.SPEED_CHECK_INTERVAL) {
entry.setSpeed(intervalDownloadedSize
/ (Constants.SPEED_CHECK_INTERVAL));
lastTime = System.currentTimeMillis();
intervalDownloadedSize = 0;
DownloadDao.getInstance(context).newOrUpdate(entry);
} else {
intervalDownloadedSize += len;
}
Trace.getInstance()
.debug(DownloadTask.class.getSimpleName(),
"onProgressChanged==>" + entry.getName() + "**"
+ entry.getPercent() + "%**" + entry.getSpeed()
+ "k/s");
}
@Override
public void onStatusChanged(DownloadStatus status) {
if (status == DownloadStatus.cancel
|| status == DownloadStatus.hijack) {
entry.setProgress(0);
entry.setPercent(0);
FileUtils.deleteFile(entry.getPath());
DownloadDao.getInstance(context).delete(entry.getUrl());
Trace.getInstance().debug(DownloadTask.class.getSimpleName(),
"onStatusChanged==>" + entry.getUrl() + " is calcled!");
}
if (status == DownloadStatus.done) {
entry.setSpeed(0);
entry.setProgress(entry.getSize());
entry.setPercent(100);
}
if (status == DownloadStatus.pause) {
entry.setSpeed(0);
}
/*********************** 处理网络异常 ***********************/
if (status == DownloadStatus.timeout
|| status == DownloadStatus.neterror) {
HashMap<String, DownloadTask> map = DataChanger.getInstance().getDownloadingTasks();
Iterator<String> iterator = map.keySet().iterator();
String key = null;
while (iterator.hasNext()) {
key = iterator.next();
DownloadTask task = map.get(key);
if (task != null) {
task.pause();
}
iterator.remove();
}
for (Entry<String, DownloadEntry> entry : DataChanger.getInstance()
.getDownloadEntries().entrySet()) {
DataChanger.getInstance().pauseDownloadEntries(entry.getKey());
DownloadDao.getInstance(context).newOrUpdate(entry.getValue());
}
}
entry.setStatus(status);
DownloadDao.getInstance(context).newOrUpdate(entry);
Message msg = handler.obtainMessage();
// handler.removeMessages(1);
msg.what = 1;
msg.obj = entry;
handler.sendMessage(msg);
Trace.getInstance().debug(DownloadTask.class.getSimpleName(),
"onStatusChanged:" + status);
}
public void cancel() {
downloadThread.cancle();
}
public void pause() {
downloadThread.pause();
}
// check download progress
// DataChanger notify progress
}