parent
bba0ccbdb1
commit
af40d3049d
@ -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; |
||||||
|
} |
||||||
|
} |
Loading…
Reference in new issue