finish the multi thread down load func and wait to test
This commit is contained in:
@@ -1,15 +1,17 @@
|
||||
package com.zhjt.mogo_core_function_devatools.upgrade
|
||||
|
||||
import android.content.Context
|
||||
import com.mogo.commons.context.ContextHolderUtil
|
||||
import com.mogo.eagle.core.function.call.devatools.CallerDevaToolsListenerManager
|
||||
import com.mogo.eagle.core.function.call.devatools.CallerDevaToolsUpgradeListenerManager
|
||||
import com.mogo.eagle.core.utilcode.breakpoint.Config
|
||||
import com.mogo.eagle.core.utilcode.breakpoint.bean.ThreadBean
|
||||
import com.mogo.eagle.core.utilcode.breakpoint.callback.IDownload
|
||||
import com.mogo.eagle.core.utilcode.breakpoint.utils.DownloadUtils
|
||||
import com.mogo.eagle.core.utilcode.mogo.logger.CallerLogger
|
||||
import com.mogo.eagle.core.utilcode.mogo.logger.scene.SceneConstant.Companion.M_DEVA
|
||||
import com.zhjt.mogo_core_function_devatools.upgrade.network.UpgradeVersionNetWorkModel
|
||||
|
||||
class UpgradeManager {
|
||||
class UpgradeManager : IDownload {
|
||||
|
||||
companion object {
|
||||
|
||||
@@ -37,13 +39,58 @@ class UpgradeManager {
|
||||
fun downLoadPackage(context: Context, downLoadKey: String) {
|
||||
// val downLoadUrl = map?.get(downLoadKey)
|
||||
// downLoadUrl?.let {
|
||||
DownloadUtils.downLoad(
|
||||
context,
|
||||
"https://scm-1255510688.cos.ap-beijing.myqcloud.com/test/com.mogo.launcher.f/2.5.1.1009/IntelligentPilot_v2.5.1.1009_20220216_[fPadLenovoOchTaxi-launcher-online]_debug.apk",
|
||||
Config.downLoadPath,
|
||||
downLoadKey,
|
||||
5
|
||||
)
|
||||
DownloadUtils.downLoad(
|
||||
context,
|
||||
"https://scm-1255510688.cos.ap-beijing.myqcloud.com/test/com.mogo.launcher.f/2.5.1.1009/IntelligentPilot_v2.5.1.1009_20220216_[fPadLenovoOchTaxi-launcher-online]_debug.apk",
|
||||
Config.downLoadPath,
|
||||
downLoadKey,
|
||||
5,
|
||||
this
|
||||
)
|
||||
// }
|
||||
}
|
||||
|
||||
override fun onStart(downloadUrl: String?) {
|
||||
CallerLogger.d("$M_DEVA$TAG","onStart downloadUrl : $downloadUrl")
|
||||
findKey(downloadUrl) {
|
||||
CallerDevaToolsUpgradeListenerManager.invokeUpgradeStart(it)
|
||||
}
|
||||
}
|
||||
|
||||
override fun onPause(downloadUrl: String?, threadBean: ThreadBean?) {
|
||||
CallerLogger.d("$M_DEVA$TAG","onPause downloadUrl : $downloadUrl , threadBean : $threadBean")
|
||||
findKey(downloadUrl) {
|
||||
CallerDevaToolsUpgradeListenerManager.invokeUpgradePause(it)
|
||||
}
|
||||
}
|
||||
|
||||
override fun onProgress(downloadUrl: String?, length: Int) {
|
||||
CallerLogger.d("$M_DEVA$TAG","onProgress downloadUrl : $downloadUrl , length : $length")
|
||||
findKey(downloadUrl) {
|
||||
CallerDevaToolsUpgradeListenerManager.invokeUpgradeProgress(it, length)
|
||||
}
|
||||
}
|
||||
|
||||
override fun onFinished(downloadUrl: String?, threadBean: ThreadBean?) {
|
||||
findKey(downloadUrl) {
|
||||
CallerDevaToolsUpgradeListenerManager.invokeUpgradeFinish(it)
|
||||
}
|
||||
}
|
||||
|
||||
override fun onError(downloadUrl: String?, errorMsg: String?) {
|
||||
CallerLogger.d("$M_DEVA$TAG","onError downloadUrl : $downloadUrl , errorMsg : $errorMsg")
|
||||
findKey(downloadUrl) {
|
||||
CallerDevaToolsUpgradeListenerManager.invokeUpgradeError(it, errorMsg ?: "未知错误")
|
||||
}
|
||||
}
|
||||
|
||||
private fun findKey(downloadUrl: String?, findResult: (downloadUrl: String) -> Unit) {
|
||||
downloadUrl?.let { url ->
|
||||
map?.iterator()?.forEach {
|
||||
if (it.value == url) {
|
||||
findResult.invoke(it.key)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -9,6 +9,10 @@ class ChainConstant {
|
||||
|
||||
const val CHAIN_LINK_LOG_CONNECT_STATUS = 0
|
||||
const val CHAIN_LINK_LOG_WEB_SOCKET_DATA = 1
|
||||
const val CHAIN_LINK_LOG_WEB_SOCKET_DATA_TRACKED = 2
|
||||
const val CHAIN_LINK_LOG_WEB_SOCKET_TRAJECTORY = 3
|
||||
const val CHAIN_LINK_LOG_WEB_SOCKET_AUTOPILOT = 4
|
||||
const val CHAIN_LINK_LOG_WEB_SOCKET_BAD_CASE = 5
|
||||
|
||||
const val CHAIN_LINK_LOG_ADAS_INIT = "-adasInitStatus"
|
||||
const val CHAIN_LINK_LOG_ADAS_MSG = "-adasWsMsg"
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
package com.mogo.eagle.core.function.api.devatools
|
||||
|
||||
interface IMogoDevaToolsUpgradeListener {
|
||||
|
||||
/**
|
||||
* 开始下载
|
||||
*/
|
||||
fun onStart(url: String?)
|
||||
|
||||
/**
|
||||
* 暂停回调
|
||||
*/
|
||||
fun onPause(url: String?)
|
||||
|
||||
/**
|
||||
* 下载进度
|
||||
*
|
||||
* @param length
|
||||
*/
|
||||
fun onProgress(url: String?, length: Int)
|
||||
|
||||
/**
|
||||
* 线程下载完毕
|
||||
*/
|
||||
fun onFinished(url: String?)
|
||||
|
||||
/**
|
||||
* 错误信息回调
|
||||
* @param errorMsg
|
||||
*/
|
||||
fun onError(url: String?, errorMsg: String?)
|
||||
}
|
||||
@@ -0,0 +1,87 @@
|
||||
package com.mogo.eagle.core.function.call.devatools
|
||||
|
||||
import androidx.annotation.Nullable
|
||||
import com.mogo.eagle.core.function.api.devatools.IMogoDevaToolsUpgradeListener
|
||||
import java.util.concurrent.ConcurrentHashMap
|
||||
|
||||
object CallerDevaToolsUpgradeListenerManager {
|
||||
|
||||
private val M_DEVA_TOOLS_UPGRADE_LISTENER: ConcurrentHashMap<String, IMogoDevaToolsUpgradeListener> =
|
||||
ConcurrentHashMap()
|
||||
|
||||
/**
|
||||
* 添加监听
|
||||
* @param tag 标记,用来注销监听使用
|
||||
* @param listener 监听回调
|
||||
*/
|
||||
fun registerDevaToolsUpgradeListener(
|
||||
@Nullable tag: String,
|
||||
@Nullable listener: IMogoDevaToolsUpgradeListener
|
||||
) {
|
||||
if (M_DEVA_TOOLS_UPGRADE_LISTENER.containsKey(tag)) {
|
||||
return
|
||||
}
|
||||
M_DEVA_TOOLS_UPGRADE_LISTENER[tag] = listener
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除监听
|
||||
* @param tag 标记,用来注销监听使用
|
||||
*/
|
||||
fun unRegisterDevaToolsUpgradeListener(@Nullable tag: String) {
|
||||
if (!M_DEVA_TOOLS_UPGRADE_LISTENER.containsKey(tag)) {
|
||||
return
|
||||
}
|
||||
M_DEVA_TOOLS_UPGRADE_LISTENER.remove(tag)
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除监听
|
||||
* @param listener 要删除的监听对象
|
||||
*/
|
||||
fun unRegisterDevaToolsUpgradeListener(@Nullable listener: IMogoDevaToolsUpgradeListener) {
|
||||
if (!M_DEVA_TOOLS_UPGRADE_LISTENER.containsValue(listener)) {
|
||||
return
|
||||
}
|
||||
M_DEVA_TOOLS_UPGRADE_LISTENER.forEach {
|
||||
if (it.value == listener) {
|
||||
M_DEVA_TOOLS_UPGRADE_LISTENER.remove(it.key)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun invokeUpgradeStart(url: String) {
|
||||
M_DEVA_TOOLS_UPGRADE_LISTENER.forEach {
|
||||
val listener = it.value
|
||||
listener.onStart(url)
|
||||
}
|
||||
}
|
||||
|
||||
fun invokeUpgradeProgress(url: String, progress: Int) {
|
||||
M_DEVA_TOOLS_UPGRADE_LISTENER.forEach {
|
||||
val listener = it.value
|
||||
listener.onProgress(url, progress)
|
||||
}
|
||||
}
|
||||
|
||||
fun invokeUpgradePause(url: String) {
|
||||
M_DEVA_TOOLS_UPGRADE_LISTENER.forEach {
|
||||
val listener = it.value
|
||||
listener.onPause(url)
|
||||
}
|
||||
}
|
||||
|
||||
fun invokeUpgradeFinish(url: String) {
|
||||
M_DEVA_TOOLS_UPGRADE_LISTENER.forEach {
|
||||
val listener = it.value
|
||||
listener.onFinished(url)
|
||||
}
|
||||
}
|
||||
|
||||
fun invokeUpgradeError(url: String, errorMsg: String) {
|
||||
M_DEVA_TOOLS_UPGRADE_LISTENER.forEach {
|
||||
val listener = it.value
|
||||
listener.onError(url, errorMsg)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -29,16 +29,6 @@
|
||||
android:resource="@xml/util_code_provider_paths" />
|
||||
</provider>
|
||||
|
||||
<service
|
||||
android:name="com.mogo.eagle.core.utilcode.util.MessengerUtils$ServerService"
|
||||
android:exported="false">
|
||||
<intent-filter>
|
||||
<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>
|
||||
@@ -2,9 +2,6 @@ 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;
|
||||
|
||||
@@ -4,24 +4,29 @@ 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 {
|
||||
|
||||
/**
|
||||
* 开始下载
|
||||
*/
|
||||
void startDownload(String url);
|
||||
|
||||
/**
|
||||
* 暂停回调
|
||||
* @param threadBean
|
||||
*/
|
||||
void pauseCallBack(ThreadBean threadBean);
|
||||
void pauseCallBack(String url,ThreadBean threadBean);
|
||||
/**
|
||||
* 下载进度
|
||||
* @param length
|
||||
*/
|
||||
void progressCallBack(int length);
|
||||
void progressCallBack(String url,int length);
|
||||
|
||||
/**
|
||||
* 线程下载完毕
|
||||
* @param threadBean
|
||||
*/
|
||||
void threadDownLoadFinished(ThreadBean threadBean);
|
||||
void threadDownLoadFinished(String url,ThreadBean threadBean);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,38 @@
|
||||
package com.mogo.eagle.core.utilcode.breakpoint.callback;
|
||||
|
||||
import com.mogo.eagle.core.utilcode.breakpoint.bean.ThreadBean;
|
||||
|
||||
public interface IDownload {
|
||||
|
||||
/**
|
||||
* 开始下载
|
||||
*/
|
||||
void onStart(String url);
|
||||
|
||||
/**
|
||||
* 暂停回调
|
||||
*
|
||||
* @param threadBean
|
||||
*/
|
||||
void onPause(String url,ThreadBean threadBean);
|
||||
|
||||
/**
|
||||
* 下载进度
|
||||
*
|
||||
* @param length
|
||||
*/
|
||||
void onProgress(String url,int length);
|
||||
|
||||
/**
|
||||
* 线程下载完毕
|
||||
*
|
||||
* @param threadBean
|
||||
*/
|
||||
void onFinished(String url,ThreadBean threadBean);
|
||||
|
||||
/**
|
||||
* 错误信息回调
|
||||
* @param errorMsg
|
||||
*/
|
||||
void onError(String url,String errorMsg);
|
||||
}
|
||||
@@ -4,9 +4,6 @@ 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";
|
||||
|
||||
@@ -13,7 +13,7 @@ import java.util.List;
|
||||
|
||||
public class ThreadDaoImpl implements ThreadDao {
|
||||
|
||||
private DBHelper dbHelper;
|
||||
private final DBHelper dbHelper;
|
||||
|
||||
public ThreadDaoImpl(Context context){
|
||||
dbHelper = DBHelper.getInstance(context);
|
||||
|
||||
@@ -1,8 +1,5 @@
|
||||
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;
|
||||
|
||||
@@ -2,82 +2,89 @@ 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.bean.ThreadBean;
|
||||
import com.mogo.eagle.core.utilcode.breakpoint.callback.DownloadCallBack;
|
||||
import com.mogo.eagle.core.utilcode.breakpoint.callback.IDownload;
|
||||
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 com.mogo.eagle.core.utilcode.util.NetworkUtils;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
|
||||
/**
|
||||
* 下载服务
|
||||
*/
|
||||
public class DownloadService extends Service implements InitThread.InitCallBack {
|
||||
public class DownloadService implements InitThread.InitCallBack, DownloadCallBack, NetworkUtils.OnNetworkStatusChangedListener {
|
||||
|
||||
public static final String ACTION_START = "ACTION_START";
|
||||
public static final String ACTION_PAUSE = "ACTION_PAUSE";
|
||||
private static final byte[] obj = new byte[0];
|
||||
private static volatile DownloadService downloadService;
|
||||
private final Context mContext;
|
||||
/**
|
||||
* 下载任务集合
|
||||
*/
|
||||
private final List<DownloadTask> downloadTasks = new ArrayList<>();
|
||||
private final Map<String, IDownload> callBacks = new ConcurrentHashMap<>();
|
||||
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);
|
||||
private DownloadService(Context context) {
|
||||
mContext = context;
|
||||
NetworkUtils.registerNetworkStatusChangedListener(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int onStartCommand(Intent intent, int flags, int startId) {
|
||||
public static DownloadService getInstance(Context context) {
|
||||
if (downloadService == null) {
|
||||
synchronized (obj) {
|
||||
if (downloadService == null) {
|
||||
downloadService = new DownloadService(context);
|
||||
}
|
||||
}
|
||||
}
|
||||
return downloadService;
|
||||
}
|
||||
|
||||
public synchronized void onStartCommand(Intent intent, IDownload iDownload) {
|
||||
if (intent == null) {
|
||||
Log.e(DOWN_LOAD_TAG, "onStartCommand intent is null");
|
||||
return Service.START_NOT_STICKY;
|
||||
return;
|
||||
}
|
||||
|
||||
String action = intent.getAction();
|
||||
if (action == null) {
|
||||
Log.e(DOWN_LOAD_TAG, "onStartCommand action is null");
|
||||
return Service.START_NOT_STICKY;
|
||||
return;
|
||||
}
|
||||
|
||||
FileBean fileBean = (FileBean) intent.getSerializableExtra("FileBean");
|
||||
if (fileBean == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
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())) {
|
||||
String url = downloadTask.getFileBean().getUrl();
|
||||
if (url.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);
|
||||
iDownload.onError(fileBean.getUrl(), "下载任务已存在");
|
||||
}
|
||||
}
|
||||
}
|
||||
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) {
|
||||
@@ -91,7 +98,6 @@ public class DownloadService extends Service implements InitThread.InitCallBack
|
||||
downloadTasks.remove(pauseTask);
|
||||
}
|
||||
}
|
||||
return super.onStartCommand(intent, flags, startId);
|
||||
}
|
||||
|
||||
//下载线程初始化完毕
|
||||
@@ -102,70 +108,82 @@ public class DownloadService extends Service implements InitThread.InitCallBack
|
||||
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());
|
||||
DownloadTask downloadTask = new DownloadTask(mContext, fileBean, fileBean.getThreadCount(), this);
|
||||
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;
|
||||
IDownload iDownload = callBacks.get(downloadData.getUrl());
|
||||
if (iDownload != null) {
|
||||
iDownload.onError(downloadData.getUrl(), "initFailed : " + downloadData.getMsg());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDestroy() {
|
||||
super.onDestroy();
|
||||
unregisterReceiver(netConnectionReceiver);
|
||||
public void startDownload(String url) {
|
||||
IDownload iDownload = callBacks.get(url);
|
||||
if (iDownload != null) {
|
||||
iDownload.onStart(url);
|
||||
}
|
||||
}
|
||||
|
||||
private class NetConnectionReceiver extends BroadcastReceiver {
|
||||
@Override
|
||||
public void pauseCallBack(String url, ThreadBean threadBean) {
|
||||
IDownload iDownload = callBacks.get(url);
|
||||
if (iDownload != null) {
|
||||
iDownload.onPause(url, threadBean);
|
||||
}
|
||||
}
|
||||
|
||||
@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);
|
||||
@Override
|
||||
public void progressCallBack(String url, int length) {
|
||||
IDownload iDownload = callBacks.get(url);
|
||||
if (iDownload != null) {
|
||||
iDownload.onProgress(url, length);
|
||||
}
|
||||
}
|
||||
|
||||
if (mobNetInfo == null || wifiNetInfo == null) {
|
||||
return;
|
||||
}
|
||||
@Override
|
||||
public void threadDownLoadFinished(String url, ThreadBean threadBean) {
|
||||
IDownload iDownload = callBacks.get(url);
|
||||
if (iDownload != null) {
|
||||
iDownload.onFinished(url, threadBean);
|
||||
}
|
||||
}
|
||||
|
||||
if (downloadTasks.size() == 0) {
|
||||
return;
|
||||
}
|
||||
@Override
|
||||
public void onConnected(NetworkUtils.NetworkType networkType) {
|
||||
//网络连接已连接
|
||||
for (DownloadTask downloadTask : downloadTasks) {
|
||||
downloadTask.startDownload();//继续所有下载任务
|
||||
String url = downloadTask.getFileBean().getUrl();
|
||||
int length = downloadTask.getFileBean().getLength();
|
||||
Log.d(DOWN_LOAD_TAG, "网络恢复继续下载");
|
||||
|
||||
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);
|
||||
}
|
||||
IDownload iDownload = callBacks.get(url);
|
||||
if (iDownload != null) {
|
||||
iDownload.onProgress(url, length);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDisconnected() {
|
||||
//网络连接已断开
|
||||
for (DownloadTask downloadTask : downloadTasks) {
|
||||
downloadTask.pauseDownload();//暂停所有下载任务
|
||||
//发送下载进度回调
|
||||
String url = downloadTask.getFileBean().getUrl();
|
||||
Log.d(DOWN_LOAD_TAG, "网络断开暂停下载");
|
||||
|
||||
IDownload iDownload = callBacks.get(url);
|
||||
if (iDownload != null) {
|
||||
iDownload.onPause(url, null);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -21,9 +21,10 @@ import java.util.List;
|
||||
*/
|
||||
public class DownloadTask implements DownloadCallBack {
|
||||
|
||||
private FileBean fileBean;
|
||||
private int downloadThreadCount;
|
||||
private ThreadDao dao;
|
||||
private final FileBean fileBean;
|
||||
private final int downloadThreadCount;
|
||||
private final ThreadDao dao;
|
||||
private final DownloadCallBack downloadCallBack;
|
||||
|
||||
/**
|
||||
* 总下载完成进度
|
||||
@@ -38,9 +39,10 @@ public class DownloadTask implements DownloadCallBack {
|
||||
*/
|
||||
private final List<DownloadThread> downloadThreads = new ArrayList<>();
|
||||
|
||||
public DownloadTask(Context context, FileBean fileBean, int downloadThreadCount) {
|
||||
public DownloadTask(Context context, FileBean fileBean, int downloadThreadCount, DownloadCallBack downloadCallBack) {
|
||||
this.fileBean = fileBean;
|
||||
this.downloadThreadCount = downloadThreadCount;
|
||||
this.downloadCallBack = downloadCallBack;
|
||||
dao = new ThreadDaoImpl(context);
|
||||
//初始化下载线程
|
||||
initDownThreads();
|
||||
@@ -49,14 +51,14 @@ public class DownloadTask implements DownloadCallBack {
|
||||
private void initDownThreads() {
|
||||
//查询数据库中的下载线程信息
|
||||
threads = dao.getThreads(fileBean.getUrl());
|
||||
if(threads.size()==0){//如果列表没有数据 则为第一次下载
|
||||
Log.w(DOWN_LOAD_TAG,"第一次下载");
|
||||
if (threads.size() == 0) {//如果列表没有数据 则为第一次下载
|
||||
Log.d(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){
|
||||
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());
|
||||
}
|
||||
//将下载线程保存到数据库
|
||||
@@ -65,13 +67,14 @@ public class DownloadTask implements DownloadCallBack {
|
||||
}
|
||||
}
|
||||
//创建下载线程开始下载
|
||||
for(ThreadBean thread : threads){
|
||||
finishedProgress+= thread.getFinished();
|
||||
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);
|
||||
downloadCallBack.startDownload(fileBean.getUrl());
|
||||
Log.d(DOWN_LOAD_TAG, " 开始下载:" + finishedProgress);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -87,58 +90,67 @@ public class DownloadTask implements DownloadCallBack {
|
||||
/**
|
||||
* 暂停下载
|
||||
*/
|
||||
public void pauseDownload(){
|
||||
for(DownloadThread downloadThread : downloadThreads){
|
||||
if (downloadThread!=null) {
|
||||
public void pauseDownload() {
|
||||
for (DownloadThread downloadThread : downloadThreads) {
|
||||
if (downloadThread != null) {
|
||||
downloadThread.setPause(true);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void pauseCallBack(ThreadBean threadBean) {
|
||||
public void startDownload(String url) {
|
||||
downloadCallBack.startDownload(url);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void pauseCallBack(String url, ThreadBean threadBean) {
|
||||
//保存下载进度到数据库
|
||||
Log.w(DOWN_LOAD_TAG,"保存数据:"+threadBean.toString());
|
||||
dao.updateThread(threadBean.getUrl(),threadBean.getId(),threadBean.getFinished());
|
||||
Log.d(DOWN_LOAD_TAG, "保存数据:" + threadBean.toString());
|
||||
dao.updateThread(threadBean.getUrl(), threadBean.getId(), threadBean.getFinished());
|
||||
downloadCallBack.pauseCallBack(url,threadBean);
|
||||
}
|
||||
|
||||
private long curTime = 0;
|
||||
|
||||
@Override
|
||||
public void progressCallBack(int length) {
|
||||
public void progressCallBack(String url, int length) {
|
||||
finishedProgress += length;
|
||||
//每500毫秒发送刷新进度事件
|
||||
if(System.currentTimeMillis() - curTime >500 || finishedProgress==fileBean.getLength()){
|
||||
if (System.currentTimeMillis() - curTime > 500 || finishedProgress == fileBean.getLength()) {
|
||||
int progress = (int) (finishedProgress * 1.0 / fileBean.getLength() * 100);
|
||||
fileBean.setFinished(finishedProgress);
|
||||
DownloadData downloadData = new DownloadData();
|
||||
downloadData.setUrl(fileBean.getUrl());
|
||||
downloadData.setProgress((int) (finishedProgress*1.0/fileBean.getLength()*100));
|
||||
downloadData.setProgress(progress);
|
||||
downloadData.setLength(fileBean.getLength());
|
||||
downloadData.setMsg("下载进度回调");
|
||||
// EventMessage message = new EventMessage(EventMessage.TYPE_PROGRESS,downloadData);
|
||||
// EventBus.getDefault().post(message);
|
||||
curTime = System.currentTimeMillis();
|
||||
|
||||
downloadCallBack.progressCallBack(url,progress);
|
||||
|
||||
curTime = System.currentTimeMillis();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public synchronized void threadDownLoadFinished(ThreadBean threadBean) {
|
||||
for(ThreadBean bean:threads){
|
||||
if(bean.getId() == threadBean.getId()){
|
||||
public synchronized void threadDownLoadFinished(String url, ThreadBean threadBean) {
|
||||
for (ThreadBean bean : threads) {
|
||||
if (bean.getId() == threadBean.getId()) {
|
||||
//从列表中将已下载完成的线程信息移除
|
||||
threads.remove(bean);
|
||||
break;
|
||||
}
|
||||
}
|
||||
if(threads.size()==0){//如果列表size为0 则所有线程已下载完成
|
||||
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);
|
||||
downloadData.setFilePath(fileBean.getSavePath() + fileBean.getFileName());
|
||||
|
||||
downloadCallBack.threadDownLoadFinished(url,threadBean);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -18,9 +18,9 @@ import java.net.URL;
|
||||
*/
|
||||
public class DownloadThread extends Thread {
|
||||
|
||||
private FileBean fileBean;
|
||||
private ThreadBean threadBean;
|
||||
private DownloadCallBack callback;
|
||||
private final FileBean fileBean;
|
||||
private final ThreadBean threadBean;
|
||||
private final DownloadCallBack callback;
|
||||
private Boolean isPause = false;
|
||||
|
||||
public DownloadThread(FileBean fileBean, ThreadBean threadBean, DownloadCallBack callback) {
|
||||
@@ -58,17 +58,17 @@ public class DownloadThread extends Thread {
|
||||
while ((len = inputStream.read(bytes))!=-1){
|
||||
raf.write(bytes,0,len);
|
||||
//将加载的进度回调出去
|
||||
callback.progressCallBack(len);
|
||||
callback.progressCallBack(this.fileBean.getUrl(),len);
|
||||
//保存进度
|
||||
threadBean.setFinished(threadBean.getFinished()+len);
|
||||
//在下载暂停的时候将下载进度保存到数据库
|
||||
if(isPause){
|
||||
callback.pauseCallBack(threadBean);
|
||||
callback.pauseCallBack(this.fileBean.getUrl(),threadBean);
|
||||
return;
|
||||
}
|
||||
}
|
||||
//下载完成
|
||||
callback.threadDownLoadFinished(threadBean);
|
||||
callback.threadDownLoadFinished(this.fileBean.getUrl(),threadBean);
|
||||
}
|
||||
|
||||
} catch (Exception e) {
|
||||
|
||||
@@ -1,9 +1,5 @@
|
||||
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;
|
||||
|
||||
@@ -15,17 +11,17 @@ import java.net.URL;
|
||||
/**
|
||||
* 初始化线程
|
||||
*/
|
||||
public class InitThread extends Thread{
|
||||
public class InitThread extends Thread {
|
||||
|
||||
private final FileBean fileBean;
|
||||
private InitCallBack initCallBack;
|
||||
private final InitCallBack initCallBack;
|
||||
|
||||
public InitThread(FileBean fileBean,InitCallBack initCallBack) {
|
||||
public InitThread(FileBean fileBean, InitCallBack initCallBack) {
|
||||
this.fileBean = fileBean;
|
||||
this.initCallBack = initCallBack;
|
||||
}
|
||||
|
||||
public interface InitCallBack{
|
||||
public interface InitCallBack {
|
||||
|
||||
void initSuccess(FileBean fileBean);
|
||||
|
||||
@@ -34,28 +30,28 @@ public class InitThread extends Thread{
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
HttpURLConnection connection ;
|
||||
RandomAccessFile randomAccessFile ;
|
||||
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){
|
||||
if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
|
||||
fileLength = connection.getContentLength();
|
||||
}
|
||||
if(fileLength<=0) return;
|
||||
if (fileLength <= 0) return;
|
||||
File dir = new File(fileBean.getSavePath());
|
||||
if(!dir.exists()){
|
||||
if (!dir.exists()) {
|
||||
dir.mkdir();
|
||||
}
|
||||
File file = new File(dir,fileBean.getFileName());
|
||||
randomAccessFile = new RandomAccessFile(file,"rwd");
|
||||
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){
|
||||
} catch (Exception e) {
|
||||
DownloadData downloadData = new DownloadData();
|
||||
downloadData.setUrl(fileBean.getUrl());
|
||||
downloadData.setMsg(e.getMessage());
|
||||
|
||||
@@ -4,25 +4,26 @@ import android.content.Context;
|
||||
import android.content.Intent;
|
||||
|
||||
import com.mogo.eagle.core.utilcode.breakpoint.bean.FileBean;
|
||||
import com.mogo.eagle.core.utilcode.breakpoint.callback.IDownload;
|
||||
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 downLoad(Context context, String url, String savePath, String fileName, int downloadThreadCount, IDownload iDownload) {
|
||||
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);
|
||||
DownloadService.getInstance(context).onStartCommand(startIntent, iDownload);
|
||||
}
|
||||
|
||||
public static void pauseDownLoad(Context context,String url){
|
||||
FileBean fileBean = new FileBean(0,null,null,url,0);
|
||||
public static void pauseDownLoad(Context context, String url, IDownload iDownload) {
|
||||
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);
|
||||
DownloadService.getInstance(context).onStartCommand(pauseIntent, iDownload);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user