1、更改部分下载类,未完成
2、@#&@*#
This commit is contained in:
@ -4,6 +4,7 @@ import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.SharedPreferences;
|
||||
import android.os.Handler;
|
||||
import android.os.Looper;
|
||||
import android.os.Message;
|
||||
import android.support.v4.util.ArrayMap;
|
||||
import android.widget.Toast;
|
||||
@ -11,13 +12,26 @@ import android.widget.Toast;
|
||||
import com.gh.common.constant.Config;
|
||||
import com.gh.common.util.DataCollectionUtils;
|
||||
import com.gh.common.util.DialogUtils;
|
||||
import com.gh.common.util.FileUtils;
|
||||
import com.gh.common.util.MD5Utils;
|
||||
import com.gh.common.util.PackageUtils;
|
||||
import com.gh.common.util.Utils;
|
||||
import com.gh.gamecenter.entity.ApkEntity;
|
||||
import com.gh.gamecenter.entity.GameEntity;
|
||||
import com.gh.gamecenter.eventbus.EBDownloadStatus;
|
||||
import com.gh.gamecenter.manager.PackageManager;
|
||||
import com.lightgame.download.ConnectionUtils;
|
||||
import com.lightgame.download.DataChanger;
|
||||
import com.lightgame.download.DataWatcher;
|
||||
import com.lightgame.download.DownloadConfig;
|
||||
import com.lightgame.download.DownloadDao;
|
||||
import com.lightgame.download.DownloadEntity;
|
||||
import com.lightgame.download.DownloadService;
|
||||
import com.lightgame.download.DownloadStatus;
|
||||
import com.lightgame.download.DownloadStatusListener;
|
||||
import com.lightgame.download.DownloadStatusManager;
|
||||
import com.lightgame.download.FileUtils;
|
||||
import com.lightgame.utils.Utils;
|
||||
|
||||
import org.greenrobot.eventbus.EventBus;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.ArrayList;
|
||||
@ -25,27 +39,75 @@ import java.util.List;
|
||||
import java.util.Map.Entry;
|
||||
import java.util.concurrent.LinkedBlockingQueue;
|
||||
|
||||
public class DownloadManager {
|
||||
public class DownloadManager implements DownloadStatusListener {
|
||||
|
||||
private static DownloadManager mInstance;
|
||||
|
||||
private Context context;
|
||||
private Handler handler;
|
||||
private Context mContext;
|
||||
private Handler mHandler;
|
||||
|
||||
private ArrayMap<String, Long> lastTimeMap;
|
||||
private ArrayMap<String, LinkedBlockingQueue<String>> platformMap;
|
||||
private ArrayMap<String, ArrayMap<String, DownloadEntity>> gameMap;
|
||||
private ArrayMap<String, String> statusMap;
|
||||
|
||||
|
||||
@Override
|
||||
public void onTaskCancelled(DownloadEntity entity) {
|
||||
EBDownloadStatus status = new EBDownloadStatus("delete", entity.getName(),
|
||||
entity.getPlatform(), entity.getUrl(), entity.getPackageName());
|
||||
status.setPluggable(entity.isPluggable());
|
||||
EventBus.getDefault().post(status);
|
||||
|
||||
DownloadNotification.showDownloadingNotification(mContext);
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onTaskAdded(DownloadEntity entity) {
|
||||
EventBus.getDefault().post(new EBDownloadStatus("download"));
|
||||
|
||||
DownloadNotification.showDownloadingNotification(mContext);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onTaskError(DownloadEntity entity) {
|
||||
DownloadNotification.showDownloadingNotification(mContext);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onTaskStatusChanged(DownloadEntity entity) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onTaskDone(DownloadEntity entity) {
|
||||
DownloadNotification.showDownloadingNotification(mContext);
|
||||
DownloadNotification.showDownloadDoneNotification(mContext, entity);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onTaskPaused(DownloadEntity entity) {
|
||||
|
||||
}
|
||||
|
||||
private DownloadManager(Context context) {
|
||||
this.context = context;
|
||||
mContext = context;
|
||||
|
||||
//TODO unregister this
|
||||
DownloadStatusManager.getInstance().registerTaskStatusListener(this);
|
||||
|
||||
// 只有下载模块需要这坨东西,因此移动到这里初始化
|
||||
ConnectionUtils.initHttpsUrlConnection(context);
|
||||
|
||||
// DownloadNotification.showDownloadingNotification(mContext);
|
||||
|
||||
lastTimeMap = new ArrayMap<>();
|
||||
platformMap = new ArrayMap<>();
|
||||
gameMap = new ArrayMap<>();
|
||||
statusMap = new ArrayMap<>();
|
||||
|
||||
handler = new Handler(context.getMainLooper()) {
|
||||
mHandler = new Handler(Looper.getMainLooper()) {
|
||||
@Override
|
||||
public void handleMessage(Message msg) {
|
||||
String url = (String) msg.obj;
|
||||
@ -148,7 +210,7 @@ public class DownloadManager {
|
||||
downloadEntity.setStatus(DownloadStatus.done);
|
||||
DataChanger.getInstance().notifyDataChanged(downloadEntity);
|
||||
} else if (!isTaskDownloading(url)) {
|
||||
context.startService(getIntent(downloadEntity, DownloadStatus.add));
|
||||
mContext.startService(getIntent(downloadEntity, DownloadStatus.add));
|
||||
}
|
||||
}
|
||||
Utils.log(DownloadManager.class.getSimpleName(), "add");
|
||||
@ -156,7 +218,11 @@ public class DownloadManager {
|
||||
|
||||
public static DownloadManager getInstance(Context context) {
|
||||
if (mInstance == null) {
|
||||
mInstance = new DownloadManager(context.getApplicationContext());
|
||||
synchronized (DownloadManager.class) {
|
||||
if (mInstance == null) {
|
||||
mInstance = new DownloadManager(context.getApplicationContext());
|
||||
}
|
||||
}
|
||||
}
|
||||
return mInstance;
|
||||
}
|
||||
@ -171,7 +237,7 @@ public class DownloadManager {
|
||||
if (entry != null && ((int) entry.getPercent()) != 0) {
|
||||
File file = new File(entry.getPath());
|
||||
if (!file.exists()) {
|
||||
DownloadDao.getInstance(context).delete(url);
|
||||
DownloadDao.getInstance(mContext).delete(url);
|
||||
Utils.log(DownloadManager.class.getSimpleName(), "文件不存在,删除该条无效数据库记录!");
|
||||
return true;
|
||||
}
|
||||
@ -207,7 +273,7 @@ public class DownloadManager {
|
||||
}
|
||||
|
||||
private Intent getIntent(DownloadEntity entry, DownloadStatus status) {
|
||||
Intent service = new Intent(context, DownloadService.class);
|
||||
Intent service = new Intent(mContext, DownloadService.class);
|
||||
service.putExtra(DownloadConfig.KEY_DOWNLOAD_ENTRY, entry);
|
||||
service.putExtra(DownloadConfig.KEY_DOWNLOAD_ACTION, status.name());
|
||||
return service;
|
||||
@ -220,7 +286,7 @@ public class DownloadManager {
|
||||
* @return null表示下载列表中不存在该任务,否则返回下载任务
|
||||
*/
|
||||
public DownloadEntity get(String url) {
|
||||
return DownloadDao.getInstance(context).get(url);
|
||||
return DownloadDao.getInstance(mContext).get(url);
|
||||
}
|
||||
|
||||
public void put(String url, long time) {
|
||||
@ -267,7 +333,7 @@ public class DownloadManager {
|
||||
*/
|
||||
public List<DownloadEntity> getAll() {
|
||||
Utils.log(DownloadManager.class.getSimpleName(), "getAll");
|
||||
return DownloadDao.getInstance(context).getAll();
|
||||
return DownloadDao.getInstance(mContext).getAll();
|
||||
}
|
||||
|
||||
public ArrayMap<String, DownloadEntity> getEntryMap(String name) {
|
||||
@ -283,7 +349,7 @@ public class DownloadManager {
|
||||
}
|
||||
|
||||
public void sendMessageDelayed(Message msg, long delayMillis) {
|
||||
handler.sendMessageDelayed(msg, delayMillis);
|
||||
mHandler.sendMessageDelayed(msg, delayMillis);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -296,7 +362,7 @@ public class DownloadManager {
|
||||
|
||||
// 暂停任务后,把文件删除,然后点继续,文件不存在,需要重新加入下载队列进行下载
|
||||
if (checkDownloadEntryRecordValidate(url)) {
|
||||
Toast.makeText(context, "文件不存在!已重新加入下载队列", Toast.LENGTH_SHORT)
|
||||
Toast.makeText(mContext, "文件不存在!已重新加入下载队列", Toast.LENGTH_SHORT)
|
||||
.show();
|
||||
add(entry);
|
||||
} else {
|
||||
@ -305,7 +371,7 @@ public class DownloadManager {
|
||||
entry.setStatus(DownloadStatus.done);
|
||||
DataChanger.getInstance().notifyDataChanged(entry);
|
||||
} else if (!isTaskDownloading(url)) {
|
||||
context.startService(getIntent(entry, DownloadStatus.resume));
|
||||
mContext.startService(getIntent(entry, DownloadStatus.resume));
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -322,17 +388,17 @@ public class DownloadManager {
|
||||
}
|
||||
|
||||
public void cancel(String url, boolean isDeleteFile) {
|
||||
DownloadEntity entry = DownloadDao.getInstance(context).get(url);
|
||||
DownloadEntity entry = DownloadDao.getInstance(mContext).get(url);
|
||||
if (entry != null) {
|
||||
if (isDeleteFile) {
|
||||
FileUtils.deleteFile(entry.getPath());
|
||||
}
|
||||
DownloadDao.getInstance(context).delete(url);
|
||||
DownloadDao.getInstance(mContext).delete(url);
|
||||
Utils.log(DownloadManager.class.getSimpleName(), "cancle==>file and record were deleted!");
|
||||
}
|
||||
if (entry != null) {
|
||||
entry.setStatus(DownloadStatus.cancel);
|
||||
context.startService(getIntent(entry, DownloadStatus.cancel));
|
||||
mContext.startService(getIntent(entry, DownloadStatus.cancel));
|
||||
Utils.log(DownloadManager.class.getSimpleName(), "cancel");
|
||||
}
|
||||
}
|
||||
@ -381,7 +447,7 @@ public class DownloadManager {
|
||||
DownloadEntity entry = DataChanger.getInstance().getDownloadEntries()
|
||||
.get(url);
|
||||
if (entry != null) {
|
||||
context.startService(getIntent(entry, DownloadStatus.pause));
|
||||
mContext.startService(getIntent(entry, DownloadStatus.pause));
|
||||
}
|
||||
Utils.log(DownloadManager.class.getSimpleName(), "pause");
|
||||
}
|
||||
@ -393,7 +459,7 @@ public class DownloadManager {
|
||||
* @return null表示下载列表中不存在该任务,否则返回下载任务
|
||||
*/
|
||||
public DownloadEntity getByPackage(String packageName) {
|
||||
for (DownloadEntity downloadEntity : DownloadDao.getInstance(context).getAll()) {
|
||||
for (DownloadEntity downloadEntity : DownloadDao.getInstance(mContext).getAll()) {
|
||||
if (packageName.equals(downloadEntity.getPackageName())) {
|
||||
return downloadEntity;
|
||||
}
|
||||
@ -405,19 +471,21 @@ public class DownloadManager {
|
||||
* 检查数据库中当前是下载状态,但并未在下载进程中的下载数据
|
||||
*/
|
||||
public void checkAll() {
|
||||
List<String> urlList = new ArrayList<>();
|
||||
for (Entry<String, DownloadTask> entry : DataChanger.getInstance()
|
||||
.getDownloadingTasks().entrySet()) {
|
||||
urlList.add(entry.getValue().getEntry().getUrl());
|
||||
}
|
||||
final List<String> urlList = new ArrayList<>(DataChanger.getInstance().getDownloadingTasks().keySet());
|
||||
for (DownloadEntity downloadEntity : getAll()) {
|
||||
if (!urlList.contains(downloadEntity.getUrl())
|
||||
&& downloadEntity.getStatus().equals(DownloadStatus.downloading)) {
|
||||
downloadEntity.setStatus(DownloadStatus.pause);
|
||||
DownloadDao.getInstance(context).newOrUpdate(downloadEntity);
|
||||
DownloadDao.getInstance(mContext).newOrUpdate(downloadEntity);
|
||||
DataChanger.getInstance().notifyDataChanged(downloadEntity);
|
||||
}
|
||||
}
|
||||
|
||||
// 开启下载服务
|
||||
mContext.startService(new Intent(mContext, DownloadService.class));
|
||||
|
||||
DownloadNotification.showDownloadingNotification(mContext);
|
||||
|
||||
}
|
||||
|
||||
public void addObserver(DataWatcher dataWatcher) {
|
||||
|
||||
Reference in New Issue
Block a user