数据库方法实现
This commit is contained in:
@ -6,8 +6,10 @@ import android.os.Handler;
|
||||
import android.util.Log;
|
||||
|
||||
import com.arialyy.downloadutil.DownloadManager;
|
||||
import com.arialyy.downloadutil.entity.DownloadEntity;
|
||||
import com.arialyy.downloadutil.inf.IDownloadListener;
|
||||
import com.arialyy.downloadutil.util.DownLoadUtil;
|
||||
import com.arialyy.downloadutil.util.SQLHelper;
|
||||
|
||||
import java.net.HttpURLConnection;
|
||||
|
||||
@ -17,21 +19,12 @@ import java.net.HttpURLConnection;
|
||||
*/
|
||||
public class Task {
|
||||
public static final String TAG = "Task";
|
||||
/**
|
||||
* 下载路径
|
||||
*/
|
||||
String downloadUrl;
|
||||
/**
|
||||
* 保存路径
|
||||
*/
|
||||
String downloadPath;
|
||||
/**
|
||||
* 下载监听
|
||||
*/
|
||||
DownloadEntity downloadEntity;
|
||||
IDownloadListener listener;
|
||||
Handler outHandler;
|
||||
Context context;
|
||||
DownLoadUtil util;
|
||||
SQLHelper sqlHelper;
|
||||
|
||||
private Task() {
|
||||
util = new DownLoadUtil();
|
||||
@ -45,9 +38,9 @@ public class Task {
|
||||
Log.d(TAG, "任务正在下载");
|
||||
} else {
|
||||
if (listener == null) {
|
||||
listener = new DownloadListener(context, outHandler);
|
||||
listener = new DownloadListener(context, downloadEntity, outHandler);
|
||||
}
|
||||
util.download(context, downloadUrl, downloadPath, listener);
|
||||
util.download(context, downloadEntity.getDownloadUrl(), downloadEntity.getDownloadPath(), listener);
|
||||
}
|
||||
}
|
||||
|
||||
@ -67,6 +60,13 @@ public class Task {
|
||||
util.cancelDownload();
|
||||
}
|
||||
|
||||
/**
|
||||
* 存储下载实体
|
||||
*/
|
||||
private void saveDownloadEntity(DownloadEntity entity) {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 下载监听类
|
||||
*/
|
||||
@ -75,11 +75,13 @@ public class Task {
|
||||
Context context;
|
||||
Intent sendIntent;
|
||||
long INTERVAL = 1024 * 10; //10k大小的间隔
|
||||
long lastLen = 0; //上一次发送长度
|
||||
long lastLen = 0; //上一次发送长度
|
||||
DownloadEntity downloadEntity;
|
||||
|
||||
public DownloadListener(Context context, Handler outHandler) {
|
||||
public DownloadListener(Context context, DownloadEntity downloadEntity, Handler outHandler) {
|
||||
this.context = context;
|
||||
this.outHandler = outHandler;
|
||||
this.downloadEntity = downloadEntity;
|
||||
sendIntent = new Intent();
|
||||
sendIntent.addCategory(context.getPackageName());
|
||||
}
|
||||
@ -87,30 +89,27 @@ public class Task {
|
||||
@Override
|
||||
public void onPreDownload(HttpURLConnection connection) {
|
||||
super.onPreDownload(connection);
|
||||
long len = connection.getContentLength();
|
||||
long len = connection.getContentLength();
|
||||
Intent preIntent = new Intent();
|
||||
preIntent.addCategory(context.getPackageName());
|
||||
|
||||
downloadEntity.setFileSize(len);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onResume(long resumeLocation) {
|
||||
super.onResume(resumeLocation);
|
||||
sendIntent.putExtra(DownloadManager.RESUME_LOCATION, resumeLocation);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onStart(long startLocation) {
|
||||
super.onStart(startLocation);
|
||||
sendIntent.putExtra(DownloadManager.START_LOCATION, startLocation);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onProgress(long currentLocation) {
|
||||
super.onProgress(currentLocation);
|
||||
if (currentLocation - lastLen > INTERVAL) { //不要太过于频繁发送广播
|
||||
sendIntent.putExtra("progress", currentLocation);
|
||||
sendBroadcast(sendIntent);
|
||||
sendIntent.putExtra(DownloadManager.ACTION_RUNNING, currentLocation);
|
||||
lastLen = currentLocation;
|
||||
}
|
||||
}
|
||||
@ -118,7 +117,6 @@ public class Task {
|
||||
@Override
|
||||
public void onStop(long stopLocation) {
|
||||
super.onStop(stopLocation);
|
||||
sendIntent.putExtra(DownloadManager.STOP_LOCATION, stopLocation);
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -138,16 +136,14 @@ public class Task {
|
||||
}
|
||||
|
||||
public static class Builder {
|
||||
String downloadUrl;
|
||||
String downloadPath;
|
||||
DownloadEntity downloadEntity;
|
||||
IDownloadListener listener;
|
||||
Handler outHandler;
|
||||
Context context;
|
||||
|
||||
public Builder(Context context, String downloadUrl, String downloadPath) {
|
||||
public Builder(Context context, DownloadEntity downloadEntity) {
|
||||
this.context = context;
|
||||
this.downloadUrl = downloadUrl;
|
||||
this.downloadPath = downloadPath;
|
||||
this.downloadEntity = downloadEntity;
|
||||
}
|
||||
|
||||
public Builder setDownloadListener(IDownloadListener listener) {
|
||||
@ -163,13 +159,11 @@ public class Task {
|
||||
public Task builder() {
|
||||
Task task = new Task();
|
||||
task.context = context;
|
||||
task.downloadUrl = downloadUrl;
|
||||
task.downloadPath = downloadPath;
|
||||
task.downloadEntity = downloadEntity;
|
||||
task.listener = listener;
|
||||
task.outHandler = outHandler;
|
||||
return task;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -0,0 +1,150 @@
|
||||
package com.arialyy.downloadutil.entity;
|
||||
|
||||
import android.os.Parcel;
|
||||
import android.os.Parcelable;
|
||||
|
||||
/**
|
||||
* Created by lyy on 2015/12/25.
|
||||
* 下载实体
|
||||
*/
|
||||
public class DownloadEntity implements Parcelable, Cloneable {
|
||||
/**
|
||||
* 其它状态
|
||||
*/
|
||||
public static final int STATE_OTHER = 7;
|
||||
/**
|
||||
* 失败状态
|
||||
*/
|
||||
public static final int STATE_FAIL = 0;
|
||||
/**
|
||||
* 完成状态
|
||||
*/
|
||||
public static final int STATE_COMPLETE = 1;
|
||||
/**
|
||||
* 停止状态
|
||||
*/
|
||||
public static final int STATE_STOP = 2;
|
||||
/**
|
||||
* 未开始状态
|
||||
*/
|
||||
public static final int STATE_WAIT = 3;
|
||||
/**
|
||||
* 未完成状态
|
||||
*/
|
||||
public static final int STATE_UN_COMPLETE = 4;
|
||||
/**
|
||||
* 下载中
|
||||
*/
|
||||
public static final int STATE_DOWNLOAD_ING = 5;
|
||||
|
||||
private String downloadUrl; //下载路径
|
||||
private String downloadPath; //保存路径
|
||||
private long completeTime; //完成时间
|
||||
private long fileSize = 1;
|
||||
private int state = STATE_WAIT;
|
||||
private boolean isDownloadComplete = false; //是否下载完成
|
||||
private long currentProgress = 0; //当前下载进度
|
||||
|
||||
public String getDownloadUrl() {
|
||||
return downloadUrl;
|
||||
}
|
||||
|
||||
public long getCompleteTime() {
|
||||
return completeTime;
|
||||
}
|
||||
|
||||
public void setCompleteTime(long completeTime) {
|
||||
this.completeTime = completeTime;
|
||||
}
|
||||
|
||||
public void setDownloadUrl(String downloadUrl) {
|
||||
this.downloadUrl = downloadUrl;
|
||||
}
|
||||
|
||||
public String getDownloadPath() {
|
||||
return downloadPath;
|
||||
}
|
||||
|
||||
public void setDownloadPath(String downloadPath) {
|
||||
this.downloadPath = downloadPath;
|
||||
}
|
||||
|
||||
public long getFileSize() {
|
||||
return fileSize;
|
||||
}
|
||||
|
||||
public void setFileSize(long fileSize) {
|
||||
this.fileSize = fileSize;
|
||||
}
|
||||
|
||||
public int getState() {
|
||||
return state;
|
||||
}
|
||||
|
||||
public void setState(int state) {
|
||||
this.state = state;
|
||||
}
|
||||
|
||||
public boolean isDownloadComplete() {
|
||||
return isDownloadComplete;
|
||||
}
|
||||
|
||||
public void setDownloadComplete(boolean downloadComplete) {
|
||||
isDownloadComplete = downloadComplete;
|
||||
}
|
||||
|
||||
public long getCurrentProgress() {
|
||||
return currentProgress;
|
||||
}
|
||||
|
||||
public void setCurrentProgress(long currentProgress) {
|
||||
this.currentProgress = currentProgress;
|
||||
}
|
||||
|
||||
@Override
|
||||
public DownloadEntity clone() throws CloneNotSupportedException {
|
||||
return (DownloadEntity) super.clone();
|
||||
}
|
||||
|
||||
|
||||
public DownloadEntity() {
|
||||
}
|
||||
|
||||
@Override
|
||||
public int describeContents() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeToParcel(Parcel dest, int flags) {
|
||||
dest.writeString(this.downloadUrl);
|
||||
dest.writeString(this.downloadPath);
|
||||
dest.writeLong(this.completeTime);
|
||||
dest.writeLong(this.fileSize);
|
||||
dest.writeInt(this.state);
|
||||
dest.writeByte(this.isDownloadComplete ? (byte) 1 : (byte) 0);
|
||||
dest.writeLong(this.currentProgress);
|
||||
}
|
||||
|
||||
protected DownloadEntity(Parcel in) {
|
||||
this.downloadUrl = in.readString();
|
||||
this.downloadPath = in.readString();
|
||||
this.completeTime = in.readLong();
|
||||
this.fileSize = in.readLong();
|
||||
this.state = in.readInt();
|
||||
this.isDownloadComplete = in.readByte() != 0;
|
||||
this.currentProgress = in.readLong();
|
||||
}
|
||||
|
||||
public static final Creator<DownloadEntity> CREATOR = new Creator<DownloadEntity>() {
|
||||
@Override
|
||||
public DownloadEntity createFromParcel(Parcel source) {
|
||||
return new DownloadEntity(source);
|
||||
}
|
||||
|
||||
@Override
|
||||
public DownloadEntity[] newArray(int size) {
|
||||
return new DownloadEntity[size];
|
||||
}
|
||||
};
|
||||
}
|
@ -0,0 +1,163 @@
|
||||
package com.arialyy.downloadutil.util;
|
||||
|
||||
import android.content.ContentValues;
|
||||
import android.content.Context;
|
||||
import android.database.Cursor;
|
||||
import android.database.sqlite.SQLiteDatabase;
|
||||
import android.database.sqlite.SQLiteOpenHelper;
|
||||
import android.support.annotation.NonNull;
|
||||
|
||||
import com.arialyy.downloadutil.entity.DownloadEntity;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Created by lyy on 2016/8/11.
|
||||
* 数据库帮助类
|
||||
*/
|
||||
public class SQLHelper extends SQLiteOpenHelper {
|
||||
public static final String DB_NAME = "ARIA_LYY_DB";
|
||||
public static final String TABLE_NAME = "ARIA_LYY_DOWNLOAD";
|
||||
|
||||
public SQLHelper(Context context) {
|
||||
this(context, DB_NAME, null, 1);
|
||||
}
|
||||
|
||||
private SQLHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
|
||||
super(context, name, factory, version);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCreate(SQLiteDatabase db) {
|
||||
String sql = "create table " + TABLE_NAME + "(" +
|
||||
"url varchar PRIMARY KEY, " +
|
||||
"path varchar, " +
|
||||
"completeTime interger, " +
|
||||
"fileSize interger, " +
|
||||
"state smallint , " +
|
||||
"isDownloadComplete smallint, " +
|
||||
"currentProgress interger" +
|
||||
")";
|
||||
db.execSQL(sql);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取所有下载实体
|
||||
*/
|
||||
public List<DownloadEntity> getAllEntity(@NonNull SQLiteDatabase db) {
|
||||
List<DownloadEntity> list = new ArrayList<>();
|
||||
Cursor c = db.query(TABLE_NAME, null, null, null, null, null, null);
|
||||
if (c.moveToFirst()) {
|
||||
while (c.moveToNext()) {
|
||||
list.add(cursor2Entity(c));
|
||||
}
|
||||
}
|
||||
c.close();
|
||||
return list;
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新下载实体
|
||||
*/
|
||||
public void updateEntity(@NonNull SQLiteDatabase db, DownloadEntity entity) {
|
||||
String whereClause = "url=?";
|
||||
String[] whereArgs = {entity.getDownloadUrl()};
|
||||
db.update(TABLE_NAME, createCv(entity), whereClause, whereArgs);
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除下载实体
|
||||
*/
|
||||
public void delEntity(@NonNull SQLiteDatabase db, DownloadEntity entity) {
|
||||
delEntity(db, entity.getDownloadUrl());
|
||||
}
|
||||
|
||||
/**
|
||||
* 通过下载链接删除下载实体
|
||||
*/
|
||||
public void delEntity(@NonNull SQLiteDatabase db, String downloadUrl) {
|
||||
String whereClause = "url=?";
|
||||
String[] whereArgs = {downloadUrl};
|
||||
db.delete(TABLE_NAME, whereClause, whereArgs);
|
||||
}
|
||||
|
||||
/**
|
||||
* 通过下载链接查找下载实体
|
||||
*
|
||||
* @param downloadUrl
|
||||
* @return
|
||||
*/
|
||||
public DownloadEntity findEntity(@NonNull SQLiteDatabase db, @NonNull String downloadUrl) {
|
||||
DownloadEntity entity;
|
||||
String sql = "select * from " + TABLE_NAME + "where url=?";
|
||||
Cursor c = db.rawQuery(sql, new String[]{downloadUrl});
|
||||
if (c.getCount() <= 0) {
|
||||
c.close();
|
||||
return null;
|
||||
} else {
|
||||
c.moveToFirst();
|
||||
entity = cursor2Entity(c);
|
||||
}
|
||||
c.close();
|
||||
return entity;
|
||||
}
|
||||
|
||||
/**
|
||||
* 存储下载实体
|
||||
*
|
||||
* @param entity
|
||||
*/
|
||||
public void savaEntity(@NonNull SQLiteDatabase db, @NonNull DownloadEntity entity) {
|
||||
DownloadEntity temp = findEntity(db, entity.getDownloadUrl());
|
||||
if (temp == null) {
|
||||
db.insert(TABLE_NAME, null, createCv(entity));
|
||||
} else {
|
||||
updateEntity(db, entity);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 游标转实体
|
||||
*
|
||||
* @param c
|
||||
* @return
|
||||
*/
|
||||
private DownloadEntity cursor2Entity(Cursor c) {
|
||||
DownloadEntity entity;
|
||||
entity = new DownloadEntity();
|
||||
entity.setDownloadUrl(c.getString(c.getColumnIndex("url")));
|
||||
entity.setDownloadPath(c.getString(c.getColumnIndex("path")));
|
||||
entity.setCompleteTime(c.getLong(c.getColumnIndex("completeTime")));
|
||||
entity.setFileSize(c.getLong(c.getColumnIndex("fileSize")));
|
||||
entity.setState(c.getInt(c.getColumnIndex("state")));
|
||||
// 0 ==> false, 1 ==> true
|
||||
entity.setDownloadComplete(c.getInt(c.getColumnIndex("isDownloadComplete")) == 0);
|
||||
entity.setCurrentProgress(c.getLong(c.getColumnIndex("currentProgress")));
|
||||
return entity;
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建ContentValues
|
||||
*
|
||||
* @param entity
|
||||
* @return
|
||||
*/
|
||||
private ContentValues createCv(@NonNull DownloadEntity entity) {
|
||||
ContentValues cv = new ContentValues();
|
||||
cv.put("url", entity.getDownloadUrl());
|
||||
cv.put("path", entity.getDownloadPath());
|
||||
cv.put("completeTime", entity.getCompleteTime());
|
||||
cv.put("fileSize", entity.getFileSize());
|
||||
cv.put("state", entity.getState());
|
||||
// 0 ==> false, 1 ==> true
|
||||
cv.put("isDownloadComplete", entity.isDownloadComplete() ? 1 : 0);
|
||||
cv.put("currentProgress", entity.getCurrentProgress());
|
||||
return cv;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user