merge
This commit is contained in:
@@ -2,6 +2,7 @@
|
||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
package="com.mogo.eagle.core.utilcode.util">
|
||||
|
||||
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
|
||||
<uses-permission android:name="android.permission.READ_PRIVILEGED_PHONE_STATE" />
|
||||
<application>
|
||||
|
||||
@@ -35,5 +36,9 @@
|
||||
<action android:name="${applicationId}.messenger" />
|
||||
</intent-filter>
|
||||
</service>
|
||||
|
||||
<service android:name="com.mogo.eagle.core.utilcode.breakpoint.services.DownloadService"/>
|
||||
|
||||
<receiver android:name=".NetworkUtils$NetworkChangedReceiver"/>
|
||||
</application>
|
||||
</manifest>
|
||||
@@ -0,0 +1,17 @@
|
||||
package com.mogo.eagle.core.utilcode.breakpoint;
|
||||
|
||||
import android.os.Environment;
|
||||
|
||||
/**
|
||||
* Created by kun on 2016/11/10.
|
||||
* 配置类
|
||||
*/
|
||||
public class Config {
|
||||
/**
|
||||
* 文件下载地址
|
||||
*/
|
||||
public final static String downLoadPath = Environment.getExternalStorageDirectory().getAbsolutePath()
|
||||
+ "/downloads/";
|
||||
|
||||
public final static String DOWN_LOAD_TAG = "Package-DownLoad";
|
||||
}
|
||||
@@ -0,0 +1,100 @@
|
||||
package com.mogo.eagle.core.utilcode.breakpoint.bean;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
public class FileBean implements Serializable {
|
||||
|
||||
private int id;
|
||||
private String savePath;
|
||||
private String fileName;
|
||||
private String url;
|
||||
private int length;
|
||||
private int finished;
|
||||
private int threadCount;
|
||||
|
||||
public FileBean() {
|
||||
}
|
||||
|
||||
public FileBean(int id, String fileName, String url, int finished) {
|
||||
this.id = id;
|
||||
this.fileName = fileName;
|
||||
this.url = url;
|
||||
this.finished = finished;
|
||||
}
|
||||
public FileBean(int id, String savePath,String fileName, String url, int finished) {
|
||||
this.id = id;
|
||||
this.savePath = savePath;
|
||||
this.fileName = fileName;
|
||||
this.url = url;
|
||||
this.finished = finished;
|
||||
}
|
||||
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(int id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getSavePath() {
|
||||
return savePath;
|
||||
}
|
||||
|
||||
public void setSavePath(String savePath) {
|
||||
this.savePath = savePath;
|
||||
}
|
||||
|
||||
public String getFileName() {
|
||||
return fileName;
|
||||
}
|
||||
|
||||
public void setFileName(String fileName) {
|
||||
this.fileName = fileName;
|
||||
}
|
||||
|
||||
public String getUrl() {
|
||||
return url;
|
||||
}
|
||||
|
||||
public void setUrl(String url) {
|
||||
this.url = url;
|
||||
}
|
||||
|
||||
public int getLength() {
|
||||
return length;
|
||||
}
|
||||
|
||||
public void setLength(int length) {
|
||||
this.length = length;
|
||||
}
|
||||
|
||||
|
||||
public int getFinished() {
|
||||
return finished;
|
||||
}
|
||||
|
||||
public void setFinished(int finished) {
|
||||
this.finished = finished;
|
||||
}
|
||||
|
||||
public int getThreadCount() {
|
||||
return threadCount;
|
||||
}
|
||||
|
||||
public void setThreadCount(int threadCount) {
|
||||
this.threadCount = threadCount;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
int progress = (int) (finished*1.0f/length *100);
|
||||
return "FileBean{" +
|
||||
"id=" + id +
|
||||
", fileName='" + fileName + '\'' +
|
||||
", length=" + length +
|
||||
", finished=" + finished +
|
||||
",progress="+progress+
|
||||
'}';
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,77 @@
|
||||
package com.mogo.eagle.core.utilcode.breakpoint.bean;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
* Created by kun on 2016/11/10.
|
||||
*/
|
||||
public class ThreadBean implements Serializable{
|
||||
|
||||
private int id;
|
||||
private String url;
|
||||
private int start;
|
||||
private int end;
|
||||
private int finished;
|
||||
|
||||
public ThreadBean() {
|
||||
}
|
||||
|
||||
public ThreadBean(int id, String url, int start, int end, int finished) {
|
||||
this.id = id;
|
||||
this.url = url;
|
||||
this.start = start;
|
||||
this.end = end;
|
||||
this.finished = finished;
|
||||
}
|
||||
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(int id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getUrl() {
|
||||
return url;
|
||||
}
|
||||
|
||||
public void setUrl(String url) {
|
||||
this.url = url;
|
||||
}
|
||||
|
||||
public int getStart() {
|
||||
return start;
|
||||
}
|
||||
|
||||
public void setStart(int start) {
|
||||
this.start = start;
|
||||
}
|
||||
|
||||
public int getEnd() {
|
||||
return end;
|
||||
}
|
||||
|
||||
public void setEnd(int end) {
|
||||
this.end = end;
|
||||
}
|
||||
|
||||
public int getFinished() {
|
||||
return finished;
|
||||
}
|
||||
|
||||
public void setFinished(int finished) {
|
||||
this.finished = finished;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "ThreadBean{" +
|
||||
"id=" + id +
|
||||
", url='" + url + '\'' +
|
||||
", start=" + start +
|
||||
", end=" + end +
|
||||
", finished=" + finished +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package com.mogo.eagle.core.utilcode.breakpoint.callback;
|
||||
|
||||
|
||||
import com.mogo.eagle.core.utilcode.breakpoint.bean.ThreadBean;
|
||||
|
||||
/**
|
||||
* Created by kun on 2016/11/11.
|
||||
* 下载进度回调
|
||||
*/
|
||||
public interface DownloadCallBack {
|
||||
/**
|
||||
* 暂停回调
|
||||
* @param threadBean
|
||||
*/
|
||||
void pauseCallBack(ThreadBean threadBean);
|
||||
/**
|
||||
* 下载进度
|
||||
* @param length
|
||||
*/
|
||||
void progressCallBack(int length);
|
||||
|
||||
/**
|
||||
* 线程下载完毕
|
||||
* @param threadBean
|
||||
*/
|
||||
void threadDownLoadFinished(ThreadBean threadBean);
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
package com.mogo.eagle.core.utilcode.breakpoint.db;
|
||||
|
||||
import android.content.Context;
|
||||
import android.database.sqlite.SQLiteDatabase;
|
||||
import android.database.sqlite.SQLiteOpenHelper;
|
||||
|
||||
/**
|
||||
* Created by kun on 2016/11/10.
|
||||
*/
|
||||
public class DBHelper extends SQLiteOpenHelper{
|
||||
|
||||
private static final String DB_NAME = "download.db";
|
||||
private static DBHelper dbHelper = null;
|
||||
|
||||
public static DBHelper getInstance(Context context){
|
||||
if(dbHelper==null) dbHelper = new DBHelper(context);
|
||||
return dbHelper;
|
||||
}
|
||||
|
||||
private DBHelper(Context context) {
|
||||
super(context, DB_NAME, null, 1);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCreate(SQLiteDatabase db) {
|
||||
String sql = "create table thread_info (_id integer primary key autoincrement," +
|
||||
"thread_id integer,url text,start integer,ends integer,finished integer)";
|
||||
db.execSQL(sql);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
package com.mogo.eagle.core.utilcode.breakpoint.db.dao;
|
||||
|
||||
|
||||
import com.mogo.eagle.core.utilcode.breakpoint.bean.ThreadBean;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface ThreadDao {
|
||||
/**
|
||||
* 插入下载线程信息
|
||||
* @param threadBean
|
||||
*/
|
||||
void insertThread(ThreadBean threadBean);
|
||||
|
||||
/**
|
||||
* 更新下载线程信息
|
||||
* @param url
|
||||
* @param thread_id
|
||||
* @param finished
|
||||
*/
|
||||
void updateThread(String url, int thread_id, int finished);
|
||||
|
||||
/**
|
||||
* 删除下载线程
|
||||
* @param url
|
||||
*/
|
||||
void deleteThread(String url);
|
||||
|
||||
/**
|
||||
* 获取下载线程
|
||||
* @param url
|
||||
* @return
|
||||
*/
|
||||
List<ThreadBean> getThreads(String url);
|
||||
|
||||
/**
|
||||
* 判断下载线程是否存在
|
||||
* @param url
|
||||
* @param thread_id
|
||||
* @return
|
||||
*/
|
||||
boolean isExists(String url, int thread_id);
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
package com.mogo.eagle.core.utilcode.breakpoint.db.impl;
|
||||
|
||||
import android.content.Context;
|
||||
import android.database.Cursor;
|
||||
import android.database.sqlite.SQLiteDatabase;
|
||||
|
||||
import com.mogo.eagle.core.utilcode.breakpoint.bean.ThreadBean;
|
||||
import com.mogo.eagle.core.utilcode.breakpoint.db.DBHelper;
|
||||
import com.mogo.eagle.core.utilcode.breakpoint.db.dao.ThreadDao;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class ThreadDaoImpl implements ThreadDao {
|
||||
|
||||
private DBHelper dbHelper;
|
||||
|
||||
public ThreadDaoImpl(Context context){
|
||||
dbHelper = DBHelper.getInstance(context);
|
||||
}
|
||||
|
||||
@Override
|
||||
public synchronized void insertThread(ThreadBean threadBean) {
|
||||
SQLiteDatabase db = dbHelper.getWritableDatabase();
|
||||
db.execSQL("insert into thread_info ( thread_id, url, start, ends, finished) values (?,?,?,?,?)"
|
||||
,new Object[]{threadBean.getId(),threadBean.getUrl(),threadBean.getStart(),threadBean.getEnd(),threadBean.getFinished()});
|
||||
db.close();
|
||||
}
|
||||
|
||||
@Override
|
||||
public synchronized void updateThread(String url, int thread_id, int finished) {
|
||||
SQLiteDatabase db = dbHelper.getWritableDatabase();
|
||||
db.execSQL("update thread_info set finished = ? where url = ? and thread_id = ?"
|
||||
,new Object[]{finished,url,thread_id});
|
||||
db.close();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void deleteThread(String url) {
|
||||
SQLiteDatabase db = dbHelper.getWritableDatabase();
|
||||
db.execSQL("delete from thread_info where url = ?",new Object[]{url});
|
||||
db.close();
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<ThreadBean> getThreads(String url) {
|
||||
SQLiteDatabase db = dbHelper.getReadableDatabase();
|
||||
Cursor cursor = db.rawQuery("select * from thread_info where url = ?",new String[]{url});
|
||||
List<ThreadBean> threadBeanList = new ArrayList<>();
|
||||
while (cursor.moveToNext()){
|
||||
ThreadBean bean = new ThreadBean();
|
||||
bean.setId(cursor.getInt(cursor.getColumnIndex("thread_id")));
|
||||
bean.setUrl(cursor.getString(cursor.getColumnIndex("url")));
|
||||
bean.setStart(cursor.getInt(cursor.getColumnIndex("start")));
|
||||
bean.setEnd(cursor.getInt(cursor.getColumnIndex("ends")));
|
||||
bean.setFinished(cursor.getInt(cursor.getColumnIndex("finished")));
|
||||
threadBeanList.add(bean);
|
||||
}
|
||||
cursor.close();
|
||||
db.close();
|
||||
return threadBeanList;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isExists(String url, int thread_id) {
|
||||
SQLiteDatabase db = dbHelper.getReadableDatabase();
|
||||
Cursor cursor = db.rawQuery("select * from thread_info where url = ? and thread_id = ?", new String[]{url,thread_id+""});
|
||||
boolean exists = cursor.moveToNext();
|
||||
cursor.close();
|
||||
db.close();
|
||||
return exists;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
package com.mogo.eagle.core.utilcode.breakpoint.event;
|
||||
|
||||
/**
|
||||
* Created by G40-70M on 2016/11/16.
|
||||
*/
|
||||
public class DownloadData {
|
||||
private String url;
|
||||
private int progress;
|
||||
private int length;
|
||||
private String msg;
|
||||
private String filePath;
|
||||
|
||||
public String getUrl() {
|
||||
return url;
|
||||
}
|
||||
|
||||
public void setUrl(String url) {
|
||||
this.url = url;
|
||||
}
|
||||
|
||||
public int getProgress() {
|
||||
return progress;
|
||||
}
|
||||
|
||||
public void setProgress(int progress) {
|
||||
this.progress = progress;
|
||||
}
|
||||
|
||||
public int getLength() {
|
||||
return length;
|
||||
}
|
||||
|
||||
public void setLength(int length) {
|
||||
this.length = length;
|
||||
}
|
||||
|
||||
public String getMsg() {
|
||||
return msg;
|
||||
}
|
||||
|
||||
public void setMsg(String msg) {
|
||||
this.msg = msg;
|
||||
}
|
||||
|
||||
public String getFilePath() {
|
||||
return filePath;
|
||||
}
|
||||
|
||||
public void setFilePath(String filePath) {
|
||||
this.filePath = filePath;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,171 @@
|
||||
package com.mogo.eagle.core.utilcode.breakpoint.services;
|
||||
|
||||
import static com.mogo.eagle.core.utilcode.breakpoint.Config.DOWN_LOAD_TAG;
|
||||
|
||||
import android.app.Service;
|
||||
import android.content.BroadcastReceiver;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.IntentFilter;
|
||||
import android.net.ConnectivityManager;
|
||||
import android.net.NetworkInfo;
|
||||
import android.os.IBinder;
|
||||
import android.util.Log;
|
||||
|
||||
import androidx.annotation.Nullable;
|
||||
|
||||
import com.mogo.eagle.core.utilcode.breakpoint.bean.FileBean;
|
||||
import com.mogo.eagle.core.utilcode.breakpoint.event.DownloadData;
|
||||
import com.mogo.eagle.core.utilcode.breakpoint.thread.DownloadTask;
|
||||
import com.mogo.eagle.core.utilcode.breakpoint.thread.InitThread;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
|
||||
/**
|
||||
* 下载服务
|
||||
*/
|
||||
public class DownloadService extends Service implements InitThread.InitCallBack {
|
||||
|
||||
public static final String ACTION_START = "ACTION_START";
|
||||
public static final String ACTION_PAUSE = "ACTION_PAUSE";
|
||||
/**
|
||||
* 下载任务集合
|
||||
*/
|
||||
private final List<DownloadTask> downloadTasks = new ArrayList<>();
|
||||
public static ExecutorService executorService = Executors.newCachedThreadPool();
|
||||
private NetConnectionReceiver netConnectionReceiver;
|
||||
|
||||
@Override
|
||||
public void onCreate() {
|
||||
super.onCreate();
|
||||
IntentFilter filter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);
|
||||
netConnectionReceiver = new NetConnectionReceiver();
|
||||
registerReceiver(netConnectionReceiver, filter);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int onStartCommand(Intent intent, int flags, int startId) {
|
||||
if (intent == null) {
|
||||
Log.e(DOWN_LOAD_TAG, "onStartCommand intent is null");
|
||||
return Service.START_NOT_STICKY;
|
||||
}
|
||||
|
||||
String action = intent.getAction();
|
||||
if (action == null) {
|
||||
Log.e(DOWN_LOAD_TAG, "onStartCommand action is null");
|
||||
return Service.START_NOT_STICKY;
|
||||
}
|
||||
|
||||
if (action.equals(ACTION_START)) {
|
||||
Log.d(DOWN_LOAD_TAG, "ACTION_START");
|
||||
FileBean fileBean = (FileBean) intent.getSerializableExtra("FileBean");
|
||||
if (downloadTasks.size() > 0) {
|
||||
for (DownloadTask downloadTask : downloadTasks) {
|
||||
if (downloadTask.getFileBean().getUrl().equals(fileBean.getUrl())) {
|
||||
//如果下载任务中以后该文件的下载任务 则直接返回
|
||||
DownloadData downloadData = new DownloadData();
|
||||
downloadData.setUrl(fileBean.getUrl());
|
||||
downloadData.setMsg("下载任务已存在");
|
||||
// EventMessage eventMessage = new EventMessage(EventMessage.TYPE_ERROR, downloadData);
|
||||
// EventBus.getDefault().post(eventMessage);
|
||||
return super.onStartCommand(intent, flags, startId);
|
||||
}
|
||||
}
|
||||
}
|
||||
executorService.execute(new InitThread(fileBean, this));
|
||||
} else if (intent.getAction().equals(ACTION_PAUSE)) {
|
||||
FileBean fileBean = (FileBean) intent.getSerializableExtra("FileBean");
|
||||
DownloadTask pauseTask = null;
|
||||
if (downloadTasks.size() > 0) {
|
||||
for (DownloadTask downloadTask : downloadTasks) {
|
||||
if (downloadTask.getFileBean().getUrl().equals(fileBean.getUrl())) {
|
||||
downloadTask.pauseDownload();
|
||||
pauseTask = downloadTask;
|
||||
break;
|
||||
}
|
||||
}
|
||||
//将下载任务移除
|
||||
downloadTasks.remove(pauseTask);
|
||||
}
|
||||
}
|
||||
return super.onStartCommand(intent, flags, startId);
|
||||
}
|
||||
|
||||
//下载线程初始化完毕
|
||||
@Override
|
||||
public void initSuccess(FileBean fileBean) {
|
||||
Log.d(DOWN_LOAD_TAG, "length:" + fileBean.getLength());
|
||||
DownloadData downloadData = new DownloadData();
|
||||
downloadData.setUrl(fileBean.getUrl());
|
||||
downloadData.setMsg("开始下载");
|
||||
downloadData.setLength(fileBean.getLength());
|
||||
// EventMessage eventMsg = new EventMessage(EventMessage.TYPE_START, downloadData);
|
||||
// EventBus.getDefault().post(eventMsg);
|
||||
//开始下载
|
||||
DownloadTask downloadTask = new DownloadTask(this, fileBean, fileBean.getThreadCount());
|
||||
downloadTasks.add(downloadTask);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void initFailed(DownloadData downloadData) {
|
||||
Log.e(DOWN_LOAD_TAG, "initFailed : " + downloadData.getMsg());
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public IBinder onBind(Intent intent) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDestroy() {
|
||||
super.onDestroy();
|
||||
unregisterReceiver(netConnectionReceiver);
|
||||
}
|
||||
|
||||
private class NetConnectionReceiver extends BroadcastReceiver {
|
||||
|
||||
@Override
|
||||
public void onReceive(Context context, Intent intent) {
|
||||
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
|
||||
NetworkInfo mobNetInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
|
||||
NetworkInfo wifiNetInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
|
||||
|
||||
if (mobNetInfo == null || wifiNetInfo == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (downloadTasks.size() == 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!mobNetInfo.isConnected() && !wifiNetInfo.isConnected()) {
|
||||
//网络连接已断开
|
||||
for (DownloadTask downloadTask : downloadTasks) {
|
||||
downloadTask.pauseDownload();//暂停所有下载任务
|
||||
//发送下载完成事件
|
||||
DownloadData downloadData = new DownloadData();
|
||||
downloadData.setUrl(downloadTask.getFileBean().getUrl());
|
||||
downloadData.setLength(downloadTask.getFileBean().getLength());
|
||||
downloadData.setMsg("网络断开暂停下载");
|
||||
// EventMessage message = new EventMessage(EventMessage.TYPE_PAUSE, downloadData);
|
||||
// EventBus.getDefault().post(message);
|
||||
}
|
||||
} else {
|
||||
//网络连接已连接
|
||||
for (DownloadTask downloadTask : downloadTasks) {
|
||||
downloadTask.startDownload();//继续所有下载任务
|
||||
DownloadData downloadData = new DownloadData();
|
||||
downloadData.setUrl(downloadTask.getFileBean().getUrl());
|
||||
downloadData.setLength(downloadTask.getFileBean().getLength());
|
||||
downloadData.setMsg("网络恢复继续下载");
|
||||
// EventMessage message = new EventMessage(EventMessage.TYPE_START, downloadData);
|
||||
// EventBus.getDefault().post(message);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,149 @@
|
||||
package com.mogo.eagle.core.utilcode.breakpoint.thread;
|
||||
|
||||
import static com.mogo.eagle.core.utilcode.breakpoint.Config.DOWN_LOAD_TAG;
|
||||
|
||||
import android.content.Context;
|
||||
import android.util.Log;
|
||||
|
||||
import com.mogo.eagle.core.utilcode.breakpoint.bean.FileBean;
|
||||
import com.mogo.eagle.core.utilcode.breakpoint.bean.ThreadBean;
|
||||
import com.mogo.eagle.core.utilcode.breakpoint.callback.DownloadCallBack;
|
||||
import com.mogo.eagle.core.utilcode.breakpoint.db.dao.ThreadDao;
|
||||
import com.mogo.eagle.core.utilcode.breakpoint.db.impl.ThreadDaoImpl;
|
||||
import com.mogo.eagle.core.utilcode.breakpoint.event.DownloadData;
|
||||
import com.mogo.eagle.core.utilcode.breakpoint.services.DownloadService;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 下载任务
|
||||
*/
|
||||
public class DownloadTask implements DownloadCallBack {
|
||||
|
||||
private FileBean fileBean;
|
||||
private int downloadThreadCount;
|
||||
private ThreadDao dao;
|
||||
|
||||
/**
|
||||
* 总下载完成进度
|
||||
*/
|
||||
private int finishedProgress = 0;
|
||||
/**
|
||||
* 下载线程信息集合
|
||||
*/
|
||||
private List<ThreadBean> threads;
|
||||
/**
|
||||
* 下载线程集合
|
||||
*/
|
||||
private final List<DownloadThread> downloadThreads = new ArrayList<>();
|
||||
|
||||
public DownloadTask(Context context, FileBean fileBean, int downloadThreadCount) {
|
||||
this.fileBean = fileBean;
|
||||
this.downloadThreadCount = downloadThreadCount;
|
||||
dao = new ThreadDaoImpl(context);
|
||||
//初始化下载线程
|
||||
initDownThreads();
|
||||
}
|
||||
|
||||
private void initDownThreads() {
|
||||
//查询数据库中的下载线程信息
|
||||
threads = dao.getThreads(fileBean.getUrl());
|
||||
if(threads.size()==0){//如果列表没有数据 则为第一次下载
|
||||
Log.w(DOWN_LOAD_TAG,"第一次下载");
|
||||
//根据下载的线程总数平分各自下载的文件长度
|
||||
int length = fileBean.getLength()/downloadThreadCount;
|
||||
for(int i = 0; i<downloadThreadCount; i++){
|
||||
ThreadBean thread = new ThreadBean(i,fileBean.getUrl(),i * length,
|
||||
(i + 1) * length -1,0);
|
||||
if(i == downloadThreadCount-1){
|
||||
thread.setEnd(fileBean.getLength());
|
||||
}
|
||||
//将下载线程保存到数据库
|
||||
dao.insertThread(thread);
|
||||
threads.add(thread);
|
||||
}
|
||||
}
|
||||
//创建下载线程开始下载
|
||||
for(ThreadBean thread : threads){
|
||||
finishedProgress+= thread.getFinished();
|
||||
DownloadThread downloadThread = new DownloadThread(fileBean, thread, this);
|
||||
DownloadService.executorService.execute(downloadThread);
|
||||
downloadThreads.add(downloadThread);
|
||||
}
|
||||
Log.w(DOWN_LOAD_TAG," 开始下载:"+finishedProgress);
|
||||
}
|
||||
|
||||
/**
|
||||
* 开始下载
|
||||
*/
|
||||
public void startDownload() {
|
||||
finishedProgress = 0;
|
||||
threads.clear();
|
||||
downloadThreads.clear();
|
||||
initDownThreads();
|
||||
}
|
||||
|
||||
/**
|
||||
* 暂停下载
|
||||
*/
|
||||
public void pauseDownload(){
|
||||
for(DownloadThread downloadThread : downloadThreads){
|
||||
if (downloadThread!=null) {
|
||||
downloadThread.setPause(true);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void pauseCallBack(ThreadBean threadBean) {
|
||||
//保存下载进度到数据库
|
||||
Log.w(DOWN_LOAD_TAG,"保存数据:"+threadBean.toString());
|
||||
dao.updateThread(threadBean.getUrl(),threadBean.getId(),threadBean.getFinished());
|
||||
}
|
||||
|
||||
private long curTime = 0;
|
||||
@Override
|
||||
public void progressCallBack(int length) {
|
||||
finishedProgress += length;
|
||||
//每500毫秒发送刷新进度事件
|
||||
if(System.currentTimeMillis() - curTime >500 || finishedProgress==fileBean.getLength()){
|
||||
fileBean.setFinished(finishedProgress);
|
||||
DownloadData downloadData = new DownloadData();
|
||||
downloadData.setUrl(fileBean.getUrl());
|
||||
downloadData.setProgress((int) (finishedProgress*1.0/fileBean.getLength()*100));
|
||||
downloadData.setLength(fileBean.getLength());
|
||||
downloadData.setMsg("下载进度回调");
|
||||
// EventMessage message = new EventMessage(EventMessage.TYPE_PROGRESS,downloadData);
|
||||
// EventBus.getDefault().post(message);
|
||||
curTime = System.currentTimeMillis();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public synchronized void threadDownLoadFinished(ThreadBean threadBean) {
|
||||
for(ThreadBean bean:threads){
|
||||
if(bean.getId() == threadBean.getId()){
|
||||
//从列表中将已下载完成的线程信息移除
|
||||
threads.remove(bean);
|
||||
break;
|
||||
}
|
||||
}
|
||||
if(threads.size()==0){//如果列表size为0 则所有线程已下载完成
|
||||
//删除数据库中的信息
|
||||
dao.deleteThread(fileBean.getUrl());
|
||||
//发送下载完成事件
|
||||
DownloadData downloadData = new DownloadData();
|
||||
downloadData.setUrl(fileBean.getUrl());
|
||||
downloadData.setMsg("下载完成");
|
||||
downloadData.setFilePath(fileBean.getSavePath()+fileBean.getFileName());
|
||||
// EventMessage message = new EventMessage(EventMessage.TYPE_FINISHED,downloadData);
|
||||
// EventBus.getDefault().post(message);
|
||||
}
|
||||
}
|
||||
|
||||
public FileBean getFileBean() {
|
||||
return fileBean;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,96 @@
|
||||
package com.mogo.eagle.core.utilcode.breakpoint.thread;
|
||||
|
||||
|
||||
import com.mogo.eagle.core.utilcode.breakpoint.Config;
|
||||
import com.mogo.eagle.core.utilcode.breakpoint.bean.FileBean;
|
||||
import com.mogo.eagle.core.utilcode.breakpoint.bean.ThreadBean;
|
||||
import com.mogo.eagle.core.utilcode.breakpoint.callback.DownloadCallBack;
|
||||
import com.mogo.eagle.core.utilcode.breakpoint.event.DownloadData;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.InputStream;
|
||||
import java.io.RandomAccessFile;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.URL;
|
||||
|
||||
/**
|
||||
* 下载线程
|
||||
*/
|
||||
public class DownloadThread extends Thread {
|
||||
|
||||
private FileBean fileBean;
|
||||
private ThreadBean threadBean;
|
||||
private DownloadCallBack callback;
|
||||
private Boolean isPause = false;
|
||||
|
||||
public DownloadThread(FileBean fileBean, ThreadBean threadBean, DownloadCallBack callback) {
|
||||
this.fileBean = fileBean;
|
||||
this.threadBean = threadBean;
|
||||
this.callback = callback;
|
||||
}
|
||||
|
||||
public void setPause(Boolean pause) {
|
||||
isPause = pause;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
HttpURLConnection connection = null;
|
||||
RandomAccessFile raf = null;
|
||||
InputStream inputStream = null;
|
||||
try {
|
||||
URL url = new URL(threadBean.getUrl());
|
||||
connection = (HttpURLConnection) url.openConnection();
|
||||
connection.setConnectTimeout(10000);
|
||||
connection.setRequestMethod("GET");
|
||||
//设置下载起始位置
|
||||
int start = threadBean.getStart() + threadBean.getFinished();
|
||||
connection.setRequestProperty("Range","bytes="+start+"-"+threadBean.getEnd());
|
||||
//设置写入位置
|
||||
File file = new File(Config.downLoadPath,fileBean.getFileName());
|
||||
raf = new RandomAccessFile(file,"rwd");
|
||||
raf.seek(start);
|
||||
//开始下载
|
||||
if(connection.getResponseCode() == HttpURLConnection.HTTP_PARTIAL){
|
||||
inputStream = connection.getInputStream();
|
||||
byte[] bytes = new byte[1024];
|
||||
int len = -1;
|
||||
while ((len = inputStream.read(bytes))!=-1){
|
||||
raf.write(bytes,0,len);
|
||||
//将加载的进度回调出去
|
||||
callback.progressCallBack(len);
|
||||
//保存进度
|
||||
threadBean.setFinished(threadBean.getFinished()+len);
|
||||
//在下载暂停的时候将下载进度保存到数据库
|
||||
if(isPause){
|
||||
callback.pauseCallBack(threadBean);
|
||||
return;
|
||||
}
|
||||
}
|
||||
//下载完成
|
||||
callback.threadDownLoadFinished(threadBean);
|
||||
}
|
||||
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
DownloadData downloadData = new DownloadData();
|
||||
downloadData.setUrl(fileBean.getUrl());
|
||||
downloadData.setMsg(e.getMessage());
|
||||
// EventMessage eventMessage = new EventMessage(EventMessage.TYPE_ERROR,downloadData);
|
||||
// EventBus.getDefault().post(eventMessage);
|
||||
} finally {
|
||||
try {
|
||||
inputStream.close();
|
||||
raf.close();
|
||||
connection.disconnect();
|
||||
}catch (Exception e){
|
||||
e.printStackTrace();
|
||||
DownloadData downloadData = new DownloadData();
|
||||
downloadData.setUrl(fileBean.getUrl());
|
||||
downloadData.setMsg(e.getMessage());
|
||||
// EventMessage eventMessage = new EventMessage(EventMessage.TYPE_ERROR,downloadData);
|
||||
// EventBus.getDefault().post(eventMessage);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
package com.mogo.eagle.core.utilcode.breakpoint.thread;
|
||||
|
||||
import static com.mogo.eagle.core.utilcode.breakpoint.Config.DOWN_LOAD_TAG;
|
||||
|
||||
import android.util.Log;
|
||||
|
||||
import com.mogo.eagle.core.utilcode.breakpoint.bean.FileBean;
|
||||
import com.mogo.eagle.core.utilcode.breakpoint.event.DownloadData;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.RandomAccessFile;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.URL;
|
||||
|
||||
/**
|
||||
* 初始化线程
|
||||
*/
|
||||
public class InitThread extends Thread{
|
||||
|
||||
private final FileBean fileBean;
|
||||
private InitCallBack initCallBack;
|
||||
|
||||
public InitThread(FileBean fileBean,InitCallBack initCallBack) {
|
||||
this.fileBean = fileBean;
|
||||
this.initCallBack = initCallBack;
|
||||
}
|
||||
|
||||
public interface InitCallBack{
|
||||
|
||||
void initSuccess(FileBean fileBean);
|
||||
|
||||
void initFailed(DownloadData downloadData);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
HttpURLConnection connection ;
|
||||
RandomAccessFile randomAccessFile ;
|
||||
try {
|
||||
URL url = new URL(fileBean.getUrl());
|
||||
connection = (HttpURLConnection) url.openConnection();
|
||||
connection.setConnectTimeout(10000);
|
||||
connection.setRequestMethod("GET");
|
||||
int fileLength = -1;
|
||||
if(connection.getResponseCode() == HttpURLConnection.HTTP_OK){
|
||||
fileLength = connection.getContentLength();
|
||||
}
|
||||
if(fileLength<=0) return;
|
||||
File dir = new File(fileBean.getSavePath());
|
||||
if(!dir.exists()){
|
||||
dir.mkdir();
|
||||
}
|
||||
File file = new File(dir,fileBean.getFileName());
|
||||
randomAccessFile = new RandomAccessFile(file,"rwd");
|
||||
randomAccessFile.setLength(fileLength);
|
||||
fileBean.setLength(fileLength);
|
||||
this.initCallBack.initSuccess(fileBean);
|
||||
}catch (Exception e){
|
||||
DownloadData downloadData = new DownloadData();
|
||||
downloadData.setUrl(fileBean.getUrl());
|
||||
downloadData.setMsg(e.getMessage());
|
||||
this.initCallBack.initFailed(downloadData);
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package com.mogo.eagle.core.utilcode.breakpoint.utils;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
|
||||
import com.mogo.eagle.core.utilcode.breakpoint.bean.FileBean;
|
||||
import com.mogo.eagle.core.utilcode.breakpoint.services.DownloadService;
|
||||
|
||||
|
||||
public class DownloadUtils {
|
||||
|
||||
public static void downLoad(Context context,String url,String savePath,String fileName,int downloadThreadCount){
|
||||
FileBean fileBean = new FileBean(0,savePath,fileName,url,0);
|
||||
fileBean.setThreadCount(downloadThreadCount);
|
||||
Intent startIntent = new Intent(context, DownloadService.class);
|
||||
startIntent.setAction(DownloadService.ACTION_START);
|
||||
startIntent.putExtra("FileBean", fileBean);
|
||||
context.startService(startIntent);
|
||||
}
|
||||
|
||||
public static void pauseDownLoad(Context context,String url){
|
||||
FileBean fileBean = new FileBean(0,null,null,url,0);
|
||||
Intent pauseIntent = new Intent(context, DownloadService.class);
|
||||
pauseIntent.setAction(DownloadService.ACTION_PAUSE);
|
||||
pauseIntent.putExtra("FileBean", fileBean);
|
||||
context.startService(pauseIntent);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user