子任务状态注解实现

pull/330/head
AriaLyy 7 years ago
parent bd16e05ed2
commit a6434dc2f1
  1. 4
      Aria/src/main/java/com/arialyy/aria/core/download/downloader/AbsGroupUtil.java
  2. 7
      Aria/src/main/java/com/arialyy/aria/core/download/downloader/HttpThreadTask.java
  3. 21
      Aria/src/main/java/com/arialyy/aria/orm/SqlHelper.java
  4. 44
      app/src/main/java/com/arialyy/simple/download/group/DownloadGroupActivity.java

@ -372,16 +372,19 @@ public abstract class AbsGroupUtil implements IUtil {
entity.setFileSize(fileSize);
entity.setConvertFileSize(CommonUtil.formatFileSize(fileSize));
saveData(IEntity.STATE_POST_PRE, -1);
mListener.onSubPre(entity);
}
@Override public void onResume(long resumeLocation) {
saveData(IEntity.STATE_POST_PRE, IEntity.STATE_RUNNING);
lastLen = resumeLocation;
mListener.onSubStart(entity);
}
@Override public void onStart(long startLocation) {
saveData(IEntity.STATE_POST_PRE, IEntity.STATE_RUNNING);
lastLen = startLocation;
mListener.onSubStart(entity);
}
@Override public void onProgress(long currentLocation) {
@ -390,6 +393,7 @@ public abstract class AbsGroupUtil implements IUtil {
lastLen = currentLocation;
entity.setCurrentProgress(currentLocation);
handleSpeed(speed);
mListener.onSubRunning(entity);
}
@Override public void onStop(long stopLocation) {

@ -24,6 +24,7 @@ import com.arialyy.aria.core.download.DownloadTaskEntity;
import com.arialyy.aria.core.inf.IDownloadListener;
import com.arialyy.aria.util.BufferedRandomAccessFile;
import com.arialyy.aria.util.CommonUtil;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
@ -45,7 +46,7 @@ final class HttpThreadTask extends AbsThreadTask<DownloadEntity, DownloadTaskEnt
@Override public void run() {
HttpURLConnection conn = null;
InputStream is = null;
BufferedInputStream is = null;
BufferedRandomAccessFile file = null;
try {
URL url = new URL(CommonUtil.convertUrl(mConfig.URL));
@ -69,7 +70,8 @@ final class HttpThreadTask extends AbsThreadTask<DownloadEntity, DownloadTaskEnt
conn = ConnectionHelp.setConnectParam(mConfig.TASK_ENTITY, conn);
conn.setConnectTimeout(STATE.CONNECT_TIME_OUT);
conn.setReadTimeout(STATE.READ_TIME_OUT); //设置读取流的等待时间,必须设置该参数
is = conn.getInputStream();
//is = conn.getInputStream();
is = new BufferedInputStream(conn.getInputStream());
//创建可设置位置的文件
file = new BufferedRandomAccessFile(mConfig.TEMP_FILE, "rwd", mBufSize);
//设置每条线程写入文件的位置
@ -84,6 +86,7 @@ final class HttpThreadTask extends AbsThreadTask<DownloadEntity, DownloadTaskEnt
if (mSleepTime > 0) Thread.sleep(mSleepTime);
file.write(buffer, 0, len);
progress(len);
Log.d(TAG, len + "");
}
if (STATE.isCancel || STATE.isStop) return;
//支持断点的处理

@ -26,6 +26,7 @@ import android.util.Log;
import com.arialyy.aria.core.AriaManager;
import com.arialyy.aria.util.CheckUtil;
import com.arialyy.aria.util.CommonUtil;
import java.io.IOException;
import java.lang.reflect.Field;
import java.lang.reflect.Type;
import java.util.ArrayList;
@ -180,7 +181,7 @@ final class SqlHelper extends SQLiteOpenHelper {
print(FIND_DATA, sql);
Cursor cursor = db.rawQuery(sql, null);
List<T> data = cursor.getCount() > 0 ? newInstanceEntity(db, clazz, cursor) : null;
cursor.close();
closeCursor(cursor);
close(db);
return data;
}
@ -223,7 +224,7 @@ final class SqlHelper extends SQLiteOpenHelper {
print(FIND_DATA, sb.toString());
Cursor cursor = db.rawQuery(sb.toString(), null);
List<T> data = cursor.getCount() > 0 ? newInstanceEntity(db, clazz, cursor) : null;
cursor.close();
closeCursor(cursor);
close(db);
return data;
}
@ -238,7 +239,7 @@ final class SqlHelper extends SQLiteOpenHelper {
print(FIND_ALL_DATA, sb.toString());
Cursor cursor = db.rawQuery(sb.toString(), null);
List<T> data = cursor.getCount() > 0 ? newInstanceEntity(db, clazz, cursor) : null;
cursor.close();
closeCursor(cursor);
close(db);
return data;
}
@ -394,7 +395,7 @@ final class SqlHelper extends SQLiteOpenHelper {
} catch (Exception e) {
e.printStackTrace();
} finally {
if (cursor != null) cursor.close();
closeCursor(cursor);
close(db);
}
return false;
@ -564,7 +565,7 @@ final class SqlHelper extends SQLiteOpenHelper {
entity.rowID = cursor.getInt(cursor.getColumnIndex("rowid"));
entitys.add(entity);
}
cursor.close();
closeCursor(cursor);
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
@ -586,6 +587,16 @@ final class SqlHelper extends SQLiteOpenHelper {
return findData(db, params[0], params[1] + "=?", primary);
}
private static void closeCursor(Cursor cursor){
if (cursor != null && !cursor.isClosed()) {
try {
cursor.close();
} catch (android.database.SQLException e) {
e.printStackTrace();
}
}
}
private static void close(SQLiteDatabase db) {
//if (db != null && db.isOpen()) db.close();
}

@ -46,7 +46,6 @@ public class DownloadGroupActivity extends BaseActivity<ActivityDownloadGroupBin
Aria.download(this).register();
setTitle("任务组");
mUrls = getModule(GroupModule.class).getUrls();
DownloadGroupEntity e = Aria.download(this).getGroupTaskList().get(0);
DownloadGroupTaskEntity entity = Aria.download(this).getDownloadGroupTask(mUrls);
if (entity != null && entity.getEntity() != null) {
DownloadGroupEntity groupEntity = entity.getEntity();
@ -98,11 +97,6 @@ public class DownloadGroupActivity extends BaseActivity<ActivityDownloadGroupBin
}
}
@DownloadGroup.onSubTaskRunning void onSubTaskRunning(DownloadGroupTask groupTask,
DownloadEntity subEntity) {
L.d(TAG, subEntity.getPercent() + "");
}
@DownloadGroup.onPre() protected void onPre(DownloadGroupTask task) {
L.d(TAG, "group pre");
}
@ -154,4 +148,42 @@ public class DownloadGroupActivity extends BaseActivity<ActivityDownloadGroupBin
T.showShort(this, "任务组下载完成");
L.d(TAG, "任务组下载完成");
}
///////////////////////////////////任务组子任务///////////////////////////////////
@DownloadGroup.onSubTaskRunning void onSubTaskRunning(DownloadGroupTask groupTask,
DownloadEntity subEntity) {
L.d(TAG, subEntity.getFileName() + "__________" + subEntity.getPercent() + "");
}
@DownloadGroup.onSubTaskPre void onSubTaskPre(DownloadGroupTask groupTask,
DownloadEntity subEntity) {
L.d(TAG, subEntity.getPercent() + "");
}
@DownloadGroup.onSubTaskStop void onSubTaskStop(DownloadGroupTask groupTask,
DownloadEntity subEntity) {
L.d(TAG, subEntity.getPercent() + "");
}
@DownloadGroup.onSubTaskStart void onSubTaskStart(DownloadGroupTask groupTask,
DownloadEntity subEntity) {
L.d(TAG, subEntity.getPercent() + "");
}
@DownloadGroup.onSubTaskCancel void onSubTaskCancel(DownloadGroupTask groupTask,
DownloadEntity subEntity) {
L.d(TAG, subEntity.getPercent() + "");
}
@DownloadGroup.onSubTaskComplete void onSubTaskComplete(DownloadGroupTask groupTask,
DownloadEntity subEntity) {
L.d(TAG, subEntity.getPercent() + "");
}
@DownloadGroup.onSubTaskFail void onSubTaskFail(DownloadGroupTask groupTask,
DownloadEntity subEntity) {
L.d(TAG, subEntity.getPercent() + "");
}
}

Loading…
Cancel
Save