修复任务组保存数据出现的问题

pull/330/head
AriaLyy 8 years ago
parent 449d9c763a
commit 2f94560c9b
  1. 9
      Aria/src/main/java/com/arialyy/aria/core/command/normal/AbsNormalCmd.java
  2. 22
      Aria/src/main/java/com/arialyy/aria/core/download/DownloadGroupEntity.java
  3. 29
      Aria/src/main/java/com/arialyy/aria/core/download/DownloadGroupTarget.java
  4. 2
      Aria/src/main/java/com/arialyy/aria/core/download/DownloadGroupTask.java
  5. 13
      Aria/src/main/java/com/arialyy/aria/core/download/downloader/FileInfoThread.java
  6. 2
      Aria/src/main/java/com/arialyy/aria/orm/DBConfig.java
  7. 2
      Aria/src/main/java/com/arialyy/aria/orm/DbEntity.java
  8. 10
      Aria/src/main/java/com/arialyy/aria/orm/SqlHelper.java
  9. 3
      app/src/main/java/com/arialyy/simple/MainActivity.java
  10. 43
      app/src/main/java/com/arialyy/simple/download/group/DownloadGroupActivity.java

@ -17,8 +17,10 @@
package com.arialyy.aria.core.command.normal; package com.arialyy.aria.core.command.normal;
import com.arialyy.aria.core.command.AbsCmd; import com.arialyy.aria.core.command.AbsCmd;
import com.arialyy.aria.core.download.DownloadGroupTaskEntity;
import com.arialyy.aria.core.download.DownloadTaskEntity; import com.arialyy.aria.core.download.DownloadTaskEntity;
import com.arialyy.aria.core.inf.AbsTaskEntity; import com.arialyy.aria.core.inf.AbsTaskEntity;
import com.arialyy.aria.core.queue.DownloadGroupTaskQueue;
import com.arialyy.aria.core.queue.DownloadTaskQueue; import com.arialyy.aria.core.queue.DownloadTaskQueue;
import com.arialyy.aria.core.queue.UploadTaskQueue; import com.arialyy.aria.core.queue.UploadTaskQueue;
import com.arialyy.aria.core.upload.UploadTaskEntity; import com.arialyy.aria.core.upload.UploadTaskEntity;
@ -40,8 +42,8 @@ public abstract class AbsNormalCmd<T extends AbsTaskEntity> extends AbsCmd<T> {
* @param targetName 产生任务的对象名 * @param targetName 产生任务的对象名
*/ */
AbsNormalCmd(String targetName, T entity) { AbsNormalCmd(String targetName, T entity) {
canExeCmd = CheckUtil.checkCmdEntity(entity, //canExeCmd = CheckUtil.checkCmdEntity(entity,
!(this instanceof CancelCmd) || !(this instanceof StopCmd)); // !(this instanceof CancelCmd) || !(this instanceof StopCmd));
mTargetName = targetName; mTargetName = targetName;
mTaskEntity = entity; mTaskEntity = entity;
TAG = CommonUtil.getClassName(this); TAG = CommonUtil.getClassName(this);
@ -51,6 +53,9 @@ public abstract class AbsNormalCmd<T extends AbsTaskEntity> extends AbsCmd<T> {
} else if (entity instanceof UploadTaskEntity) { } else if (entity instanceof UploadTaskEntity) {
mQueue = UploadTaskQueue.getInstance(); mQueue = UploadTaskQueue.getInstance();
isDownloadCmd = false; isDownloadCmd = false;
} else if (entity instanceof DownloadGroupTaskEntity) {
mQueue = DownloadGroupTaskQueue.getInstance();
isDownloadCmd = true;
} }
} }
} }

@ -19,7 +19,6 @@ import android.os.Parcel;
import com.arialyy.aria.core.inf.AbsGroupEntity; import com.arialyy.aria.core.inf.AbsGroupEntity;
import com.arialyy.aria.orm.OneToMany; import com.arialyy.aria.orm.OneToMany;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List; import java.util.List;
/** /**
@ -28,26 +27,25 @@ import java.util.List;
*/ */
public class DownloadGroupEntity extends AbsGroupEntity { public class DownloadGroupEntity extends AbsGroupEntity {
@OneToMany(table = DownloadEntity.class, key = "groupName") private List<DownloadEntity> @OneToMany(table = DownloadEntity.class, key = "groupName") private List<DownloadEntity> subtask = new ArrayList<>();
mSubtask = new ArrayList<>();
//任务组下载文件的文件夹地址 //任务组下载文件的文件夹地址
private String mDirPath = ""; private String dirPath = "";
public List<DownloadEntity> getSubTask() { public List<DownloadEntity> getSubTask() {
return mSubtask; return subtask;
} }
public void setSubTasks(List<DownloadEntity> subTasks) { public void setSubTasks(List<DownloadEntity> subTasks) {
this.mSubtask = subTasks; this.subtask = subTasks;
} }
public String getDirPath() { public String getDirPath() {
return mDirPath; return dirPath;
} }
public void setDirPath(String dirPath) { public void setDirPath(String dirPath) {
this.mDirPath = dirPath; this.dirPath = dirPath;
} }
public DownloadGroupEntity() { public DownloadGroupEntity() {
@ -59,14 +57,14 @@ public class DownloadGroupEntity extends AbsGroupEntity {
@Override public void writeToParcel(Parcel dest, int flags) { @Override public void writeToParcel(Parcel dest, int flags) {
super.writeToParcel(dest, flags); super.writeToParcel(dest, flags);
dest.writeTypedList(this.mSubtask); dest.writeTypedList(this.subtask);
dest.writeString(this.mDirPath); dest.writeString(this.dirPath);
} }
protected DownloadGroupEntity(Parcel in) { protected DownloadGroupEntity(Parcel in) {
super(in); super(in);
this.mSubtask = in.createTypedArrayList(DownloadEntity.CREATOR); this.subtask = in.createTypedArrayList(DownloadEntity.CREATOR);
this.mDirPath = in.readString(); this.dirPath = in.readString();
} }
public static final Creator<DownloadGroupEntity> CREATOR = new Creator<DownloadGroupEntity>() { public static final Creator<DownloadGroupEntity> CREATOR = new Creator<DownloadGroupEntity>() {

@ -50,7 +50,8 @@ public class DownloadGroupTarget
if (mTaskEntity == null) { if (mTaskEntity == null) {
mTaskEntity = new DownloadGroupTaskEntity(); mTaskEntity = new DownloadGroupTaskEntity();
mTaskEntity.key = mGroupName; mTaskEntity.key = mGroupName;
mTaskEntity.entity = new DownloadGroupEntity(); mTaskEntity.entity = getDownloadGroupEntity();
mTaskEntity.insert();
} }
if (mTaskEntity.entity == null) { if (mTaskEntity.entity == null) {
mTaskEntity.entity = getDownloadGroupEntity(); mTaskEntity.entity = getDownloadGroupEntity();
@ -63,6 +64,9 @@ public class DownloadGroupTarget
DbEntity.findFirst(DownloadGroupEntity.class, "groupName=?", mGroupName); DbEntity.findFirst(DownloadGroupEntity.class, "groupName=?", mGroupName);
if (entity == null) { if (entity == null) {
entity = new DownloadGroupEntity(); entity = new DownloadGroupEntity();
entity.setGroupName(mGroupName);
entity.setUrlmd5(mGroupName);
entity.insert();
} }
return entity; return entity;
} }
@ -101,12 +105,12 @@ public class DownloadGroupTarget
file.mkdirs(); file.mkdirs();
} }
mEntity.setDirPath(groupDirPath);
if (!TextUtils.isEmpty(mEntity.getDirPath())) { if (!TextUtils.isEmpty(mEntity.getDirPath())) {
reChangeDirPath(groupDirPath); reChangeDirPath(groupDirPath);
} else { } else {
mEntity.setSubTasks(createSubTask()); mEntity.setSubTasks(createSubTask());
} }
mEntity.setDirPath(groupDirPath);
mEntity.update(); mEntity.update();
isSetDirPathed = true; isSetDirPathed = true;
return this; return this;
@ -157,15 +161,26 @@ public class DownloadGroupTarget
for (int i = 0, len = mUrls.size(); i < len; i++) { for (int i = 0, len = mUrls.size(); i < len; i++) {
DownloadEntity entity = new DownloadEntity(); DownloadEntity entity = new DownloadEntity();
entity.setDownloadUrl(mUrls.get(i)); entity.setDownloadUrl(mUrls.get(i));
String fileName = mSubtaskFileName.get(i); String fileName = mSubtaskFileName.isEmpty() ? CommonUtil.keyToHashKey(mUrls.get(i))
if (TextUtils.isEmpty(fileName)) { : mSubtaskFileName.get(i);
fileName = CommonUtil.keyToHashKey(mUrls.get(i));
}
entity.setDownloadPath(mEntity.getDirPath() + "/" + fileName); entity.setDownloadPath(mEntity.getDirPath() + "/" + fileName);
entity.setGroupName(mGroupName); entity.setGroupName(mGroupName);
entity.save(); entity.setGroupChild(true);
entity.setFileName(fileName);
entity.insert();
list.add(entity); list.add(entity);
} }
return list; return list;
} }
///**
// * 创建文件名,如果url链接有后缀名,则使用url中的后缀名
// * @return url 的 hashKey
// */
//private String createFileName(String url){
// if (url.contains(".")){
// int s = url.lastIndexOf(".");
// String fileName = url.substring()
// }
//}
} }

@ -51,7 +51,7 @@ public class DownloadGroupTask extends AbsGroupTask<DownloadGroupTaskEntity, Dow
} }
@Override public void start() { @Override public void start() {
mUtil.stopDownload(); mUtil.startDownload();
} }
@Override public void stop() { @Override public void stop() {

@ -15,6 +15,7 @@
*/ */
package com.arialyy.aria.core.download.downloader; package com.arialyy.aria.core.download.downloader;
import android.text.TextUtils;
import android.util.Log; import android.util.Log;
import com.arialyy.aria.core.AriaManager; import com.arialyy.aria.core.AriaManager;
import com.arialyy.aria.core.download.DownloadEntity; import com.arialyy.aria.core.download.DownloadEntity;
@ -117,8 +118,8 @@ class FileInfoThread implements Runnable {
if (onFileInfoListener != null) { if (onFileInfoListener != null) {
onFileInfoListener.onComplete(mEntity.getDownloadUrl(), code); onFileInfoListener.onComplete(mEntity.getDownloadUrl(), code);
} }
mEntity.save(); mEntity.update();
mTaskEntity.save(); mTaskEntity.update();
} }
} }
@ -127,7 +128,13 @@ class FileInfoThread implements Runnable {
*/ */
private void handle302Turn(HttpURLConnection conn) throws IOException { private void handle302Turn(HttpURLConnection conn) throws IOException {
String newUrl = conn.getHeaderField(mTaskEntity.redirectUrlKey); String newUrl = conn.getHeaderField(mTaskEntity.redirectUrlKey);
Log.d(TAG, "30x跳转,新url为【" + newUrl + "】"); Log.d(TAG, "30x跳转,location【 " + mTaskEntity.redirectUrlKey + "】" + "新url为【" + newUrl + "】");
if (TextUtils.isEmpty(newUrl) || newUrl.equalsIgnoreCase("null")) {
if (onFileInfoListener != null) {
onFileInfoListener.onFail(mEntity.getDownloadUrl(), "获取重定向链接失败");
}
return;
}
String cookies = conn.getHeaderField("Set-Cookie"); String cookies = conn.getHeaderField("Set-Cookie");
conn = (HttpURLConnection) new URL(newUrl).openConnection(); conn = (HttpURLConnection) new URL(newUrl).openConnection();
conn = ConnectionHelp.setConnectParam(mTaskEntity, conn); conn = ConnectionHelp.setConnectParam(mTaskEntity, conn);

@ -32,7 +32,7 @@ import java.util.Map;
public class DBConfig { public class DBConfig {
static Map<String, Class> mapping = new HashMap<>(); static Map<String, Class> mapping = new HashMap<>();
static String DB_NAME; static String DB_NAME;
static int VERSION = 6; static int VERSION = 7;
static { static {
if (TextUtils.isEmpty(DB_NAME)) { if (TextUtils.isEmpty(DB_NAME)) {

@ -125,7 +125,7 @@ public class DbEntity {
*/ */
public void save() { public void save() {
synchronized (LOCK) { synchronized (LOCK) {
if (DbUtil.getInstance().tableExists(getClass()) && thisIsExist()) { if (thisIsExist()) {
update(); update();
} else { } else {
insert(); insert();

@ -373,6 +373,14 @@ final class SqlHelper extends SQLiteOpenHelper {
close(db); close(db);
} }
/**
* 保存一对一的数据
* @param field
*/
private void saveOneToOneFile(Field field){
}
/** /**
* 获取一对一参数 * 获取一对一参数
*/ */
@ -602,7 +610,7 @@ final class SqlHelper extends SQLiteOpenHelper {
if (TextUtils.isEmpty(primaryData)) continue; if (TextUtils.isEmpty(primaryData)) continue;
List<T> list = findForeignData(db, primaryData, params); List<T> list = findForeignData(db, primaryData, params);
if (list == null) continue; if (list == null) continue;
field.set(entity, findForeignData(db, primaryKey, params)); field.set(entity, findForeignData(db, primaryData, params));
} else if (isOneToOne(field)) { } else if (isOneToOne(field)) {
String primaryKey = getPrimaryName(clazz); String primaryKey = getPrimaryName(clazz);
if (TextUtils.isEmpty(primaryKey)) { if (TextUtils.isEmpty(primaryKey)) {

@ -23,6 +23,7 @@ import butterknife.OnClick;
import com.arialyy.simple.base.BaseActivity; import com.arialyy.simple.base.BaseActivity;
import com.arialyy.simple.databinding.ActivityMainBinding; import com.arialyy.simple.databinding.ActivityMainBinding;
import com.arialyy.simple.download.DownloadActivity; import com.arialyy.simple.download.DownloadActivity;
import com.arialyy.simple.download.group.DownloadGroupActivity;
import com.arialyy.simple.test.TestMutilTaskSysDownload; import com.arialyy.simple.test.TestMutilTaskSysDownload;
import com.arialyy.simple.upload.UploadActivity; import com.arialyy.simple.upload.UploadActivity;
@ -53,7 +54,7 @@ public class MainActivity extends BaseActivity<ActivityMainBinding> {
startActivity(new Intent(this, UploadActivity.class)); startActivity(new Intent(this, UploadActivity.class));
break; break;
case R.id.download_task_group: case R.id.download_task_group:
startActivity(new Intent(this, DownloadGroupActivity.class));
break; break;
} }

@ -61,26 +61,25 @@ public class DownloadGroupActivity extends BaseActivity<ActivityDownloadGroupBin
//}); //});
} }
//public void onClick(View view) { public void onClick(View view) {
// switch (view.getId()) { switch (view.getId()) {
// case R.id.start: case R.id.start:
// String text = ((TextView) view).getText().toString(); String text = ((TextView) view).getText().toString();
// if (text.equals("重新开始?") || text.equals("开始")) { if (text.equals("重新开始?") || text.equals("开始")) {
// Aria.download(this) Aria.download(this)
// .load(mUrls) .load(mUrls)
// .setDownloadPaths() .setDownloadDirPath(Environment.getExternalStorageDirectory().getPath() + "/group_test")
// .setDownloadPath(Environment.getExternalStorageDirectory().getPath() + "/test.apk") .start();
// .start(); } else if (text.equals("恢复")) {
// } else if (text.equals("恢复")) { //Aria.download(this).load(DOWNLOAD_URL).resume();
// Aria.download(this).load(DOWNLOAD_URL).resume(); }
// } break;
// break; case R.id.stop:
// case R.id.stop: //Aria.download(this).load(DOWNLOAD_URL).pause();
// Aria.download(this).load(DOWNLOAD_URL).pause(); break;
// break; case R.id.cancel:
// case R.id.cancel: //Aria.download(this).load(DOWNLOAD_URL).cancel();
// Aria.download(this).load(DOWNLOAD_URL).cancel(); break;
// break; }
// } }
//}
} }

Loading…
Cancel
Save