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
|
package com.zhjt.mogo_core_function_devatools.upgrade
|
||||||
|
|
||||||
import android.content.Context
|
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.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.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.breakpoint.utils.DownloadUtils
|
||||||
import com.mogo.eagle.core.utilcode.mogo.logger.CallerLogger
|
import com.mogo.eagle.core.utilcode.mogo.logger.CallerLogger
|
||||||
import com.mogo.eagle.core.utilcode.mogo.logger.scene.SceneConstant.Companion.M_DEVA
|
import com.mogo.eagle.core.utilcode.mogo.logger.scene.SceneConstant.Companion.M_DEVA
|
||||||
import com.zhjt.mogo_core_function_devatools.upgrade.network.UpgradeVersionNetWorkModel
|
import com.zhjt.mogo_core_function_devatools.upgrade.network.UpgradeVersionNetWorkModel
|
||||||
|
|
||||||
class UpgradeManager {
|
class UpgradeManager : IDownload {
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
|
|
||||||
@@ -37,13 +39,58 @@ class UpgradeManager {
|
|||||||
fun downLoadPackage(context: Context, downLoadKey: String) {
|
fun downLoadPackage(context: Context, downLoadKey: String) {
|
||||||
// val downLoadUrl = map?.get(downLoadKey)
|
// val downLoadUrl = map?.get(downLoadKey)
|
||||||
// downLoadUrl?.let {
|
// downLoadUrl?.let {
|
||||||
DownloadUtils.downLoad(
|
DownloadUtils.downLoad(
|
||||||
context,
|
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",
|
"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,
|
Config.downLoadPath,
|
||||||
downLoadKey,
|
downLoadKey,
|
||||||
5
|
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_CONNECT_STATUS = 0
|
||||||
const val CHAIN_LINK_LOG_WEB_SOCKET_DATA = 1
|
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_INIT = "-adasInitStatus"
|
||||||
const val CHAIN_LINK_LOG_ADAS_MSG = "-adasWsMsg"
|
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" />
|
android:resource="@xml/util_code_provider_paths" />
|
||||||
</provider>
|
</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"/>
|
<receiver android:name=".NetworkUtils$NetworkChangedReceiver"/>
|
||||||
</application>
|
</application>
|
||||||
</manifest>
|
</manifest>
|
||||||
@@ -2,9 +2,6 @@ package com.mogo.eagle.core.utilcode.breakpoint.bean;
|
|||||||
|
|
||||||
import java.io.Serializable;
|
import java.io.Serializable;
|
||||||
|
|
||||||
/**
|
|
||||||
* Created by kun on 2016/11/10.
|
|
||||||
*/
|
|
||||||
public class ThreadBean implements Serializable{
|
public class ThreadBean implements Serializable{
|
||||||
|
|
||||||
private int id;
|
private int id;
|
||||||
|
|||||||
@@ -4,24 +4,29 @@ package com.mogo.eagle.core.utilcode.breakpoint.callback;
|
|||||||
import com.mogo.eagle.core.utilcode.breakpoint.bean.ThreadBean;
|
import com.mogo.eagle.core.utilcode.breakpoint.bean.ThreadBean;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Created by kun on 2016/11/11.
|
|
||||||
* 下载进度回调
|
* 下载进度回调
|
||||||
*/
|
*/
|
||||||
public interface DownloadCallBack {
|
public interface DownloadCallBack {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 开始下载
|
||||||
|
*/
|
||||||
|
void startDownload(String url);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 暂停回调
|
* 暂停回调
|
||||||
* @param threadBean
|
* @param threadBean
|
||||||
*/
|
*/
|
||||||
void pauseCallBack(ThreadBean threadBean);
|
void pauseCallBack(String url,ThreadBean threadBean);
|
||||||
/**
|
/**
|
||||||
* 下载进度
|
* 下载进度
|
||||||
* @param length
|
* @param length
|
||||||
*/
|
*/
|
||||||
void progressCallBack(int length);
|
void progressCallBack(String url,int length);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 线程下载完毕
|
* 线程下载完毕
|
||||||
* @param threadBean
|
* @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.SQLiteDatabase;
|
||||||
import android.database.sqlite.SQLiteOpenHelper;
|
import android.database.sqlite.SQLiteOpenHelper;
|
||||||
|
|
||||||
/**
|
|
||||||
* Created by kun on 2016/11/10.
|
|
||||||
*/
|
|
||||||
public class DBHelper extends SQLiteOpenHelper{
|
public class DBHelper extends SQLiteOpenHelper{
|
||||||
|
|
||||||
private static final String DB_NAME = "download.db";
|
private static final String DB_NAME = "download.db";
|
||||||
|
|||||||
@@ -13,7 +13,7 @@ import java.util.List;
|
|||||||
|
|
||||||
public class ThreadDaoImpl implements ThreadDao {
|
public class ThreadDaoImpl implements ThreadDao {
|
||||||
|
|
||||||
private DBHelper dbHelper;
|
private final DBHelper dbHelper;
|
||||||
|
|
||||||
public ThreadDaoImpl(Context context){
|
public ThreadDaoImpl(Context context){
|
||||||
dbHelper = DBHelper.getInstance(context);
|
dbHelper = DBHelper.getInstance(context);
|
||||||
|
|||||||
@@ -1,8 +1,5 @@
|
|||||||
package com.mogo.eagle.core.utilcode.breakpoint.event;
|
package com.mogo.eagle.core.utilcode.breakpoint.event;
|
||||||
|
|
||||||
/**
|
|
||||||
* Created by G40-70M on 2016/11/16.
|
|
||||||
*/
|
|
||||||
public class DownloadData {
|
public class DownloadData {
|
||||||
private String url;
|
private String url;
|
||||||
private int progress;
|
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 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.Context;
|
||||||
import android.content.Intent;
|
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 android.util.Log;
|
||||||
|
|
||||||
import androidx.annotation.Nullable;
|
|
||||||
|
|
||||||
import com.mogo.eagle.core.utilcode.breakpoint.bean.FileBean;
|
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.event.DownloadData;
|
||||||
import com.mogo.eagle.core.utilcode.breakpoint.thread.DownloadTask;
|
import com.mogo.eagle.core.utilcode.breakpoint.thread.DownloadTask;
|
||||||
import com.mogo.eagle.core.utilcode.breakpoint.thread.InitThread;
|
import com.mogo.eagle.core.utilcode.breakpoint.thread.InitThread;
|
||||||
|
import com.mogo.eagle.core.utilcode.util.NetworkUtils;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
import java.util.concurrent.ExecutorService;
|
import java.util.concurrent.ExecutorService;
|
||||||
import java.util.concurrent.Executors;
|
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_START = "ACTION_START";
|
||||||
public static final String ACTION_PAUSE = "ACTION_PAUSE";
|
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 List<DownloadTask> downloadTasks = new ArrayList<>();
|
||||||
|
private final Map<String, IDownload> callBacks = new ConcurrentHashMap<>();
|
||||||
public static ExecutorService executorService = Executors.newCachedThreadPool();
|
public static ExecutorService executorService = Executors.newCachedThreadPool();
|
||||||
private NetConnectionReceiver netConnectionReceiver;
|
|
||||||
|
|
||||||
@Override
|
private DownloadService(Context context) {
|
||||||
public void onCreate() {
|
mContext = context;
|
||||||
super.onCreate();
|
NetworkUtils.registerNetworkStatusChangedListener(this);
|
||||||
IntentFilter filter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);
|
|
||||||
netConnectionReceiver = new NetConnectionReceiver();
|
|
||||||
registerReceiver(netConnectionReceiver, filter);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
public static DownloadService getInstance(Context context) {
|
||||||
public int onStartCommand(Intent intent, int flags, int startId) {
|
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) {
|
if (intent == null) {
|
||||||
Log.e(DOWN_LOAD_TAG, "onStartCommand intent is null");
|
Log.e(DOWN_LOAD_TAG, "onStartCommand intent is null");
|
||||||
return Service.START_NOT_STICKY;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
String action = intent.getAction();
|
String action = intent.getAction();
|
||||||
if (action == null) {
|
if (action == null) {
|
||||||
Log.e(DOWN_LOAD_TAG, "onStartCommand action is 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)) {
|
if (action.equals(ACTION_START)) {
|
||||||
Log.d(DOWN_LOAD_TAG, "ACTION_START");
|
Log.d(DOWN_LOAD_TAG, "ACTION_START");
|
||||||
FileBean fileBean = (FileBean) intent.getSerializableExtra("FileBean");
|
|
||||||
if (downloadTasks.size() > 0) {
|
if (downloadTasks.size() > 0) {
|
||||||
for (DownloadTask downloadTask : downloadTasks) {
|
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();
|
iDownload.onError(fileBean.getUrl(), "下载任务已存在");
|
||||||
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));
|
executorService.execute(new InitThread(fileBean, this));
|
||||||
} else if (intent.getAction().equals(ACTION_PAUSE)) {
|
} else if (intent.getAction().equals(ACTION_PAUSE)) {
|
||||||
FileBean fileBean = (FileBean) intent.getSerializableExtra("FileBean");
|
|
||||||
DownloadTask pauseTask = null;
|
DownloadTask pauseTask = null;
|
||||||
if (downloadTasks.size() > 0) {
|
if (downloadTasks.size() > 0) {
|
||||||
for (DownloadTask downloadTask : downloadTasks) {
|
for (DownloadTask downloadTask : downloadTasks) {
|
||||||
@@ -91,7 +98,6 @@ public class DownloadService extends Service implements InitThread.InitCallBack
|
|||||||
downloadTasks.remove(pauseTask);
|
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.setUrl(fileBean.getUrl());
|
||||||
downloadData.setMsg("开始下载");
|
downloadData.setMsg("开始下载");
|
||||||
downloadData.setLength(fileBean.getLength());
|
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);
|
downloadTasks.add(downloadTask);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void initFailed(DownloadData downloadData) {
|
public void initFailed(DownloadData downloadData) {
|
||||||
Log.e(DOWN_LOAD_TAG, "initFailed : " + downloadData.getMsg());
|
Log.e(DOWN_LOAD_TAG, "initFailed : " + downloadData.getMsg());
|
||||||
}
|
IDownload iDownload = callBacks.get(downloadData.getUrl());
|
||||||
|
if (iDownload != null) {
|
||||||
@Nullable
|
iDownload.onError(downloadData.getUrl(), "initFailed : " + downloadData.getMsg());
|
||||||
@Override
|
}
|
||||||
public IBinder onBind(Intent intent) {
|
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onDestroy() {
|
public void startDownload(String url) {
|
||||||
super.onDestroy();
|
IDownload iDownload = callBacks.get(url);
|
||||||
unregisterReceiver(netConnectionReceiver);
|
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
|
@Override
|
||||||
public void onReceive(Context context, Intent intent) {
|
public void progressCallBack(String url, int length) {
|
||||||
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
|
IDownload iDownload = callBacks.get(url);
|
||||||
NetworkInfo mobNetInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
|
if (iDownload != null) {
|
||||||
NetworkInfo wifiNetInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
|
iDownload.onProgress(url, length);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (mobNetInfo == null || wifiNetInfo == null) {
|
@Override
|
||||||
return;
|
public void threadDownLoadFinished(String url, ThreadBean threadBean) {
|
||||||
}
|
IDownload iDownload = callBacks.get(url);
|
||||||
|
if (iDownload != null) {
|
||||||
|
iDownload.onFinished(url, threadBean);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (downloadTasks.size() == 0) {
|
@Override
|
||||||
return;
|
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()) {
|
IDownload iDownload = callBacks.get(url);
|
||||||
//网络连接已断开
|
if (iDownload != null) {
|
||||||
for (DownloadTask downloadTask : downloadTasks) {
|
iDownload.onProgress(url, length);
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@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 {
|
public class DownloadTask implements DownloadCallBack {
|
||||||
|
|
||||||
private FileBean fileBean;
|
private final FileBean fileBean;
|
||||||
private int downloadThreadCount;
|
private final int downloadThreadCount;
|
||||||
private ThreadDao dao;
|
private final ThreadDao dao;
|
||||||
|
private final DownloadCallBack downloadCallBack;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 总下载完成进度
|
* 总下载完成进度
|
||||||
@@ -38,9 +39,10 @@ public class DownloadTask implements DownloadCallBack {
|
|||||||
*/
|
*/
|
||||||
private final List<DownloadThread> downloadThreads = new ArrayList<>();
|
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.fileBean = fileBean;
|
||||||
this.downloadThreadCount = downloadThreadCount;
|
this.downloadThreadCount = downloadThreadCount;
|
||||||
|
this.downloadCallBack = downloadCallBack;
|
||||||
dao = new ThreadDaoImpl(context);
|
dao = new ThreadDaoImpl(context);
|
||||||
//初始化下载线程
|
//初始化下载线程
|
||||||
initDownThreads();
|
initDownThreads();
|
||||||
@@ -49,14 +51,14 @@ public class DownloadTask implements DownloadCallBack {
|
|||||||
private void initDownThreads() {
|
private void initDownThreads() {
|
||||||
//查询数据库中的下载线程信息
|
//查询数据库中的下载线程信息
|
||||||
threads = dao.getThreads(fileBean.getUrl());
|
threads = dao.getThreads(fileBean.getUrl());
|
||||||
if(threads.size()==0){//如果列表没有数据 则为第一次下载
|
if (threads.size() == 0) {//如果列表没有数据 则为第一次下载
|
||||||
Log.w(DOWN_LOAD_TAG,"第一次下载");
|
Log.d(DOWN_LOAD_TAG, "第一次下载");
|
||||||
//根据下载的线程总数平分各自下载的文件长度
|
//根据下载的线程总数平分各自下载的文件长度
|
||||||
int length = fileBean.getLength()/downloadThreadCount;
|
int length = fileBean.getLength() / downloadThreadCount;
|
||||||
for(int i = 0; i<downloadThreadCount; i++){
|
for (int i = 0; i < downloadThreadCount; i++) {
|
||||||
ThreadBean thread = new ThreadBean(i,fileBean.getUrl(),i * length,
|
ThreadBean thread = new ThreadBean(i, fileBean.getUrl(), i * length,
|
||||||
(i + 1) * length -1,0);
|
(i + 1) * length - 1, 0);
|
||||||
if(i == downloadThreadCount-1){
|
if (i == downloadThreadCount - 1) {
|
||||||
thread.setEnd(fileBean.getLength());
|
thread.setEnd(fileBean.getLength());
|
||||||
}
|
}
|
||||||
//将下载线程保存到数据库
|
//将下载线程保存到数据库
|
||||||
@@ -65,13 +67,14 @@ public class DownloadTask implements DownloadCallBack {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
//创建下载线程开始下载
|
//创建下载线程开始下载
|
||||||
for(ThreadBean thread : threads){
|
for (ThreadBean thread : threads) {
|
||||||
finishedProgress+= thread.getFinished();
|
finishedProgress += thread.getFinished();
|
||||||
DownloadThread downloadThread = new DownloadThread(fileBean, thread, this);
|
DownloadThread downloadThread = new DownloadThread(fileBean, thread, this);
|
||||||
DownloadService.executorService.execute(downloadThread);
|
DownloadService.executorService.execute(downloadThread);
|
||||||
downloadThreads.add(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(){
|
public void pauseDownload() {
|
||||||
for(DownloadThread downloadThread : downloadThreads){
|
for (DownloadThread downloadThread : downloadThreads) {
|
||||||
if (downloadThread!=null) {
|
if (downloadThread != null) {
|
||||||
downloadThread.setPause(true);
|
downloadThread.setPause(true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@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());
|
Log.d(DOWN_LOAD_TAG, "保存数据:" + threadBean.toString());
|
||||||
dao.updateThread(threadBean.getUrl(),threadBean.getId(),threadBean.getFinished());
|
dao.updateThread(threadBean.getUrl(), threadBean.getId(), threadBean.getFinished());
|
||||||
|
downloadCallBack.pauseCallBack(url,threadBean);
|
||||||
}
|
}
|
||||||
|
|
||||||
private long curTime = 0;
|
private long curTime = 0;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void progressCallBack(int length) {
|
public void progressCallBack(String url, int length) {
|
||||||
finishedProgress += length;
|
finishedProgress += length;
|
||||||
//每500毫秒发送刷新进度事件
|
//每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);
|
fileBean.setFinished(finishedProgress);
|
||||||
DownloadData downloadData = new DownloadData();
|
DownloadData downloadData = new DownloadData();
|
||||||
downloadData.setUrl(fileBean.getUrl());
|
downloadData.setUrl(fileBean.getUrl());
|
||||||
downloadData.setProgress((int) (finishedProgress*1.0/fileBean.getLength()*100));
|
downloadData.setProgress(progress);
|
||||||
downloadData.setLength(fileBean.getLength());
|
downloadData.setLength(fileBean.getLength());
|
||||||
downloadData.setMsg("下载进度回调");
|
downloadData.setMsg("下载进度回调");
|
||||||
// EventMessage message = new EventMessage(EventMessage.TYPE_PROGRESS,downloadData);
|
|
||||||
// EventBus.getDefault().post(message);
|
downloadCallBack.progressCallBack(url,progress);
|
||||||
curTime = System.currentTimeMillis();
|
|
||||||
|
curTime = System.currentTimeMillis();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public synchronized void threadDownLoadFinished(ThreadBean threadBean) {
|
public synchronized void threadDownLoadFinished(String url, ThreadBean threadBean) {
|
||||||
for(ThreadBean bean:threads){
|
for (ThreadBean bean : threads) {
|
||||||
if(bean.getId() == threadBean.getId()){
|
if (bean.getId() == threadBean.getId()) {
|
||||||
//从列表中将已下载完成的线程信息移除
|
//从列表中将已下载完成的线程信息移除
|
||||||
threads.remove(bean);
|
threads.remove(bean);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if(threads.size()==0){//如果列表size为0 则所有线程已下载完成
|
if (threads.size() == 0) {//如果列表size为0 则所有线程已下载完成
|
||||||
//删除数据库中的信息
|
//删除数据库中的信息
|
||||||
dao.deleteThread(fileBean.getUrl());
|
dao.deleteThread(fileBean.getUrl());
|
||||||
//发送下载完成事件
|
//发送下载完成事件
|
||||||
DownloadData downloadData = new DownloadData();
|
DownloadData downloadData = new DownloadData();
|
||||||
downloadData.setUrl(fileBean.getUrl());
|
downloadData.setUrl(fileBean.getUrl());
|
||||||
downloadData.setMsg("下载完成");
|
downloadData.setMsg("下载完成");
|
||||||
downloadData.setFilePath(fileBean.getSavePath()+fileBean.getFileName());
|
downloadData.setFilePath(fileBean.getSavePath() + fileBean.getFileName());
|
||||||
// EventMessage message = new EventMessage(EventMessage.TYPE_FINISHED,downloadData);
|
|
||||||
// EventBus.getDefault().post(message);
|
downloadCallBack.threadDownLoadFinished(url,threadBean);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -18,9 +18,9 @@ import java.net.URL;
|
|||||||
*/
|
*/
|
||||||
public class DownloadThread extends Thread {
|
public class DownloadThread extends Thread {
|
||||||
|
|
||||||
private FileBean fileBean;
|
private final FileBean fileBean;
|
||||||
private ThreadBean threadBean;
|
private final ThreadBean threadBean;
|
||||||
private DownloadCallBack callback;
|
private final DownloadCallBack callback;
|
||||||
private Boolean isPause = false;
|
private Boolean isPause = false;
|
||||||
|
|
||||||
public DownloadThread(FileBean fileBean, ThreadBean threadBean, DownloadCallBack callback) {
|
public DownloadThread(FileBean fileBean, ThreadBean threadBean, DownloadCallBack callback) {
|
||||||
@@ -58,17 +58,17 @@ public class DownloadThread extends Thread {
|
|||||||
while ((len = inputStream.read(bytes))!=-1){
|
while ((len = inputStream.read(bytes))!=-1){
|
||||||
raf.write(bytes,0,len);
|
raf.write(bytes,0,len);
|
||||||
//将加载的进度回调出去
|
//将加载的进度回调出去
|
||||||
callback.progressCallBack(len);
|
callback.progressCallBack(this.fileBean.getUrl(),len);
|
||||||
//保存进度
|
//保存进度
|
||||||
threadBean.setFinished(threadBean.getFinished()+len);
|
threadBean.setFinished(threadBean.getFinished()+len);
|
||||||
//在下载暂停的时候将下载进度保存到数据库
|
//在下载暂停的时候将下载进度保存到数据库
|
||||||
if(isPause){
|
if(isPause){
|
||||||
callback.pauseCallBack(threadBean);
|
callback.pauseCallBack(this.fileBean.getUrl(),threadBean);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
//下载完成
|
//下载完成
|
||||||
callback.threadDownLoadFinished(threadBean);
|
callback.threadDownLoadFinished(this.fileBean.getUrl(),threadBean);
|
||||||
}
|
}
|
||||||
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
|
|||||||
@@ -1,9 +1,5 @@
|
|||||||
package com.mogo.eagle.core.utilcode.breakpoint.thread;
|
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.bean.FileBean;
|
||||||
import com.mogo.eagle.core.utilcode.breakpoint.event.DownloadData;
|
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 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.fileBean = fileBean;
|
||||||
this.initCallBack = initCallBack;
|
this.initCallBack = initCallBack;
|
||||||
}
|
}
|
||||||
|
|
||||||
public interface InitCallBack{
|
public interface InitCallBack {
|
||||||
|
|
||||||
void initSuccess(FileBean fileBean);
|
void initSuccess(FileBean fileBean);
|
||||||
|
|
||||||
@@ -34,28 +30,28 @@ public class InitThread extends Thread{
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
HttpURLConnection connection ;
|
HttpURLConnection connection;
|
||||||
RandomAccessFile randomAccessFile ;
|
RandomAccessFile randomAccessFile;
|
||||||
try {
|
try {
|
||||||
URL url = new URL(fileBean.getUrl());
|
URL url = new URL(fileBean.getUrl());
|
||||||
connection = (HttpURLConnection) url.openConnection();
|
connection = (HttpURLConnection) url.openConnection();
|
||||||
connection.setConnectTimeout(10000);
|
connection.setConnectTimeout(10000);
|
||||||
connection.setRequestMethod("GET");
|
connection.setRequestMethod("GET");
|
||||||
int fileLength = -1;
|
int fileLength = -1;
|
||||||
if(connection.getResponseCode() == HttpURLConnection.HTTP_OK){
|
if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
|
||||||
fileLength = connection.getContentLength();
|
fileLength = connection.getContentLength();
|
||||||
}
|
}
|
||||||
if(fileLength<=0) return;
|
if (fileLength <= 0) return;
|
||||||
File dir = new File(fileBean.getSavePath());
|
File dir = new File(fileBean.getSavePath());
|
||||||
if(!dir.exists()){
|
if (!dir.exists()) {
|
||||||
dir.mkdir();
|
dir.mkdir();
|
||||||
}
|
}
|
||||||
File file = new File(dir,fileBean.getFileName());
|
File file = new File(dir, fileBean.getFileName());
|
||||||
randomAccessFile = new RandomAccessFile(file,"rwd");
|
randomAccessFile = new RandomAccessFile(file, "rwd");
|
||||||
randomAccessFile.setLength(fileLength);
|
randomAccessFile.setLength(fileLength);
|
||||||
fileBean.setLength(fileLength);
|
fileBean.setLength(fileLength);
|
||||||
this.initCallBack.initSuccess(fileBean);
|
this.initCallBack.initSuccess(fileBean);
|
||||||
}catch (Exception e){
|
} catch (Exception e) {
|
||||||
DownloadData downloadData = new DownloadData();
|
DownloadData downloadData = new DownloadData();
|
||||||
downloadData.setUrl(fileBean.getUrl());
|
downloadData.setUrl(fileBean.getUrl());
|
||||||
downloadData.setMsg(e.getMessage());
|
downloadData.setMsg(e.getMessage());
|
||||||
|
|||||||
@@ -4,25 +4,26 @@ import android.content.Context;
|
|||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
|
|
||||||
import com.mogo.eagle.core.utilcode.breakpoint.bean.FileBean;
|
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;
|
import com.mogo.eagle.core.utilcode.breakpoint.services.DownloadService;
|
||||||
|
|
||||||
|
|
||||||
public class DownloadUtils {
|
public class DownloadUtils {
|
||||||
|
|
||||||
public static void downLoad(Context context,String url,String savePath,String fileName,int downloadThreadCount){
|
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 fileBean = new FileBean(0, savePath, fileName, url, 0);
|
||||||
fileBean.setThreadCount(downloadThreadCount);
|
fileBean.setThreadCount(downloadThreadCount);
|
||||||
Intent startIntent = new Intent(context, DownloadService.class);
|
Intent startIntent = new Intent(context, DownloadService.class);
|
||||||
startIntent.setAction(DownloadService.ACTION_START);
|
startIntent.setAction(DownloadService.ACTION_START);
|
||||||
startIntent.putExtra("FileBean", fileBean);
|
startIntent.putExtra("FileBean", fileBean);
|
||||||
context.startService(startIntent);
|
DownloadService.getInstance(context).onStartCommand(startIntent, iDownload);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void pauseDownLoad(Context context,String url){
|
public static void pauseDownLoad(Context context, String url, IDownload iDownload) {
|
||||||
FileBean fileBean = new FileBean(0,null,null,url,0);
|
FileBean fileBean = new FileBean(0, null, null, url, 0);
|
||||||
Intent pauseIntent = new Intent(context, DownloadService.class);
|
Intent pauseIntent = new Intent(context, DownloadService.class);
|
||||||
pauseIntent.setAction(DownloadService.ACTION_PAUSE);
|
pauseIntent.setAction(DownloadService.ACTION_PAUSE);
|
||||||
pauseIntent.putExtra("FileBean", fileBean);
|
pauseIntent.putExtra("FileBean", fileBean);
|
||||||
context.startService(pauseIntent);
|
DownloadService.getInstance(context).onStartCommand(pauseIntent, iDownload);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user