添加302 支持,orm模型支持更新
This commit is contained in:
6
.idea/vcs.xml
generated
6
.idea/vcs.xml
generated
@ -1,6 +0,0 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
|
||||||
<project version="4">
|
|
||||||
<component name="VcsDirectoryMappings">
|
|
||||||
<mapping directory="" vcs="Git" />
|
|
||||||
</component>
|
|
||||||
</project>
|
|
@ -2,7 +2,7 @@ apply plugin: 'com.android.library'
|
|||||||
|
|
||||||
android {
|
android {
|
||||||
compileSdkVersion 23
|
compileSdkVersion 23
|
||||||
buildToolsVersion "23.0.2"
|
buildToolsVersion '25.0.2'
|
||||||
|
|
||||||
defaultConfig {
|
defaultConfig {
|
||||||
minSdkVersion 9
|
minSdkVersion 9
|
||||||
|
@ -27,7 +27,6 @@ import android.content.Context;
|
|||||||
import android.os.Build;
|
import android.os.Build;
|
||||||
import android.widget.PopupWindow;
|
import android.widget.PopupWindow;
|
||||||
import com.arialyy.aria.core.download.DownloadReceiver;
|
import com.arialyy.aria.core.download.DownloadReceiver;
|
||||||
import com.arialyy.aria.core.scheduler.DownloadSchedulers;
|
|
||||||
import com.arialyy.aria.core.scheduler.IDownloadSchedulerListener;
|
import com.arialyy.aria.core.scheduler.IDownloadSchedulerListener;
|
||||||
import com.arialyy.aria.core.scheduler.ISchedulerListener;
|
import com.arialyy.aria.core.scheduler.ISchedulerListener;
|
||||||
import com.arialyy.aria.core.download.DownloadTask;
|
import com.arialyy.aria.core.download.DownloadTask;
|
||||||
@ -207,8 +206,8 @@ import com.arialyy.aria.core.upload.UploadTask;
|
|||||||
/**
|
/**
|
||||||
* 下载任务状态监听
|
* 下载任务状态监听
|
||||||
*/
|
*/
|
||||||
public static class DownloadSchedulerListener implements
|
public static class DownloadSchedulerListener
|
||||||
IDownloadSchedulerListener<DownloadTask> {
|
implements IDownloadSchedulerListener<DownloadTask> {
|
||||||
|
|
||||||
@Override public void onTaskPre(DownloadTask task) {
|
@Override public void onTaskPre(DownloadTask task) {
|
||||||
|
|
||||||
|
@ -257,7 +257,10 @@ import java.util.Map;
|
|||||||
} else if (obj instanceof Application) {
|
} else if (obj instanceof Application) {
|
||||||
key = clsName;
|
key = clsName;
|
||||||
}
|
}
|
||||||
} else {
|
}
|
||||||
|
if (obj instanceof Activity || obj instanceof Service) {
|
||||||
|
key = clsName;
|
||||||
|
} else if (obj instanceof Application) {
|
||||||
key = clsName;
|
key = clsName;
|
||||||
}
|
}
|
||||||
if (TextUtils.isEmpty(key)) {
|
if (TextUtils.isEmpty(key)) {
|
||||||
|
@ -77,6 +77,9 @@ class ConnectionHelp {
|
|||||||
"image/gif, image/jpeg, image/pjpeg, image/pjpeg, application/x-shockwave-flash, application/xaml+xml, application/vnd.ms-xpsdocument, application/x-ms-xbap, application/x-ms-application, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*");
|
"image/gif, image/jpeg, image/pjpeg, image/pjpeg, application/x-shockwave-flash, application/xaml+xml, application/vnd.ms-xpsdocument, application/x-ms-xbap, application/x-ms-application, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*");
|
||||||
////用于处理Disconnect 不起作用问题
|
////用于处理Disconnect 不起作用问题
|
||||||
//conn.setRequestProperty("Connection", "close");
|
//conn.setRequestProperty("Connection", "close");
|
||||||
|
conn.setRequestProperty("Connection", "Keep-Alive");
|
||||||
|
//302获取重定向地址
|
||||||
|
conn.setInstanceFollowRedirects(false);
|
||||||
return conn;
|
return conn;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -30,15 +30,6 @@ import com.arialyy.aria.orm.DbEntity;
|
|||||||
* !!!并且需要Parcelable时需要手动填写rowID;
|
* !!!并且需要Parcelable时需要手动填写rowID;
|
||||||
*/
|
*/
|
||||||
public class DownloadEntity extends DbEntity implements Parcelable, IEntity {
|
public class DownloadEntity extends DbEntity implements Parcelable, IEntity {
|
||||||
@Ignore 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];
|
|
||||||
}
|
|
||||||
};
|
|
||||||
@Ignore private long speed = 0; //下载速度
|
@Ignore private long speed = 0; //下载速度
|
||||||
@Ignore private int failNum = 0;
|
@Ignore private int failNum = 0;
|
||||||
private String downloadUrl = ""; //下载路径
|
private String downloadUrl = ""; //下载路径
|
||||||
@ -50,25 +41,12 @@ public class DownloadEntity extends DbEntity implements Parcelable, IEntity {
|
|||||||
private boolean isDownloadComplete = false; //是否下载完成
|
private boolean isDownloadComplete = false; //是否下载完成
|
||||||
private long currentProgress = 0; //当前下载进度
|
private long currentProgress = 0; //当前下载进度
|
||||||
private long completeTime; //完成时间
|
private long completeTime; //完成时间
|
||||||
|
private boolean isRedirect = false;
|
||||||
|
private String redirectUrl = ""; //重定向链接
|
||||||
|
|
||||||
public DownloadEntity() {
|
public DownloadEntity() {
|
||||||
}
|
}
|
||||||
|
|
||||||
protected DownloadEntity(Parcel in) {
|
|
||||||
this.downloadUrl = in.readString();
|
|
||||||
this.downloadPath = in.readString();
|
|
||||||
this.fileName = in.readString();
|
|
||||||
this.str = in.readString();
|
|
||||||
this.completeTime = in.readLong();
|
|
||||||
this.fileSize = in.readLong();
|
|
||||||
this.state = in.readInt();
|
|
||||||
this.isDownloadComplete = in.readByte() != 0;
|
|
||||||
this.currentProgress = in.readLong();
|
|
||||||
this.failNum = in.readInt();
|
|
||||||
this.speed = in.readLong();
|
|
||||||
this.rowID = in.readInt();
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getStr() {
|
public String getStr() {
|
||||||
return str;
|
return str;
|
||||||
}
|
}
|
||||||
@ -164,16 +142,40 @@ public class DownloadEntity extends DbEntity implements Parcelable, IEntity {
|
|||||||
return (DownloadEntity) super.clone();
|
return (DownloadEntity) super.clone();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public boolean isRedirect() {
|
||||||
|
return isRedirect;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setRedirect(boolean redirect) {
|
||||||
|
isRedirect = redirect;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getRedirectUrl() {
|
||||||
|
return redirectUrl;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setRedirectUrl(String redirectUrl) {
|
||||||
|
this.redirectUrl = redirectUrl;
|
||||||
|
}
|
||||||
|
|
||||||
@Override public String toString() {
|
@Override public String toString() {
|
||||||
return "DownloadEntity{"
|
return "DownloadEntity{"
|
||||||
+ "downloadUrl='"
|
+ "speed="
|
||||||
|
+ speed
|
||||||
|
+ ", failNum="
|
||||||
|
+ failNum
|
||||||
|
+ ", downloadUrl='"
|
||||||
+ downloadUrl
|
+ downloadUrl
|
||||||
+ '\''
|
+ '\''
|
||||||
+ ", downloadPath='"
|
+ ", downloadPath='"
|
||||||
+ downloadPath
|
+ downloadPath
|
||||||
+ '\''
|
+ '\''
|
||||||
+ ", completeTime="
|
+ ", fileName='"
|
||||||
+ completeTime
|
+ fileName
|
||||||
|
+ '\''
|
||||||
|
+ ", str='"
|
||||||
|
+ str
|
||||||
|
+ '\''
|
||||||
+ ", fileSize="
|
+ ", fileSize="
|
||||||
+ fileSize
|
+ fileSize
|
||||||
+ ", state="
|
+ ", state="
|
||||||
@ -182,8 +184,13 @@ public class DownloadEntity extends DbEntity implements Parcelable, IEntity {
|
|||||||
+ isDownloadComplete
|
+ isDownloadComplete
|
||||||
+ ", currentProgress="
|
+ ", currentProgress="
|
||||||
+ currentProgress
|
+ currentProgress
|
||||||
+ ", failNum="
|
+ ", completeTime="
|
||||||
+ failNum
|
+ completeTime
|
||||||
|
+ ", isRedirect="
|
||||||
|
+ isRedirect
|
||||||
|
+ ", redirectUrl='"
|
||||||
|
+ redirectUrl
|
||||||
|
+ '\''
|
||||||
+ '}';
|
+ '}';
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -192,17 +199,44 @@ public class DownloadEntity extends DbEntity implements Parcelable, IEntity {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override public void writeToParcel(Parcel dest, int flags) {
|
@Override public void writeToParcel(Parcel dest, int flags) {
|
||||||
|
dest.writeLong(this.speed);
|
||||||
|
dest.writeInt(this.failNum);
|
||||||
dest.writeString(this.downloadUrl);
|
dest.writeString(this.downloadUrl);
|
||||||
dest.writeString(this.downloadPath);
|
dest.writeString(this.downloadPath);
|
||||||
dest.writeString(this.fileName);
|
dest.writeString(this.fileName);
|
||||||
dest.writeString(this.str);
|
dest.writeString(this.str);
|
||||||
dest.writeLong(this.completeTime);
|
|
||||||
dest.writeLong(this.fileSize);
|
dest.writeLong(this.fileSize);
|
||||||
dest.writeInt(this.state);
|
dest.writeInt(this.state);
|
||||||
dest.writeByte(this.isDownloadComplete ? (byte) 1 : (byte) 0);
|
dest.writeByte(this.isDownloadComplete ? (byte) 1 : (byte) 0);
|
||||||
dest.writeLong(this.currentProgress);
|
dest.writeLong(this.currentProgress);
|
||||||
dest.writeInt(this.failNum);
|
dest.writeLong(this.completeTime);
|
||||||
dest.writeLong(this.speed);
|
dest.writeByte(this.isRedirect ? (byte) 1 : (byte) 0);
|
||||||
dest.writeInt(this.rowID);
|
dest.writeString(this.redirectUrl);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected DownloadEntity(Parcel in) {
|
||||||
|
this.speed = in.readLong();
|
||||||
|
this.failNum = in.readInt();
|
||||||
|
this.downloadUrl = in.readString();
|
||||||
|
this.downloadPath = in.readString();
|
||||||
|
this.fileName = in.readString();
|
||||||
|
this.str = in.readString();
|
||||||
|
this.fileSize = in.readLong();
|
||||||
|
this.state = in.readInt();
|
||||||
|
this.isDownloadComplete = in.readByte() != 0;
|
||||||
|
this.currentProgress = in.readLong();
|
||||||
|
this.completeTime = in.readLong();
|
||||||
|
this.isRedirect = in.readByte() != 0;
|
||||||
|
this.redirectUrl = in.readString();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Ignore 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];
|
||||||
|
}
|
||||||
|
};
|
||||||
}
|
}
|
@ -17,6 +17,7 @@ package com.arialyy.aria.core.download;
|
|||||||
|
|
||||||
import android.support.annotation.NonNull;
|
import android.support.annotation.NonNull;
|
||||||
import android.text.TextUtils;
|
import android.text.TextUtils;
|
||||||
|
import android.util.Log;
|
||||||
import com.arialyy.aria.core.RequestEnum;
|
import com.arialyy.aria.core.RequestEnum;
|
||||||
import com.arialyy.aria.core.inf.AbsTarget;
|
import com.arialyy.aria.core.inf.AbsTarget;
|
||||||
import com.arialyy.aria.core.queue.DownloadTaskQueue;
|
import com.arialyy.aria.core.queue.DownloadTaskQueue;
|
||||||
@ -44,6 +45,13 @@ public class DownloadTarget extends AbsTarget<DownloadEntity, DownloadTaskEntity
|
|||||||
super.resume();
|
super.resume();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 重定向后,新url的key,默认为location
|
||||||
|
*/
|
||||||
|
public void setRedirectUrlKey(String redirectUrlKey) {
|
||||||
|
super._setRedirectUrlKey(redirectUrlKey);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 给url请求添加头部
|
* 给url请求添加头部
|
||||||
*
|
*
|
||||||
|
@ -246,9 +246,11 @@ public class DownloadTask implements ITask {
|
|||||||
|
|
||||||
@Override public void supportBreakpoint(boolean support) {
|
@Override public void supportBreakpoint(boolean support) {
|
||||||
super.supportBreakpoint(support);
|
super.supportBreakpoint(support);
|
||||||
|
if (!support) {
|
||||||
sendInState2Target(ISchedulers.SUPPORT_BREAK_POINT);
|
sendInState2Target(ISchedulers.SUPPORT_BREAK_POINT);
|
||||||
sendIntent(Aria.ACTION_SUPPORT_BREAK_POINT, -1);
|
sendIntent(Aria.ACTION_SUPPORT_BREAK_POINT, -1);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Override public void onPre() {
|
@Override public void onPre() {
|
||||||
super.onPre();
|
super.onPre();
|
||||||
|
@ -34,7 +34,7 @@ import java.util.concurrent.Executors;
|
|||||||
* Created by lyy on 2015/8/25.
|
* Created by lyy on 2015/8/25.
|
||||||
* 下载工具类
|
* 下载工具类
|
||||||
*/
|
*/
|
||||||
final class DownloadUtil implements IDownloadUtil, Runnable {
|
public class DownloadUtil implements IDownloadUtil, Runnable {
|
||||||
private static final String TAG = "DownloadUtil";
|
private static final String TAG = "DownloadUtil";
|
||||||
/**
|
/**
|
||||||
* 线程数
|
* 线程数
|
||||||
@ -207,6 +207,21 @@ final class DownloadUtil implements IDownloadUtil, Runnable {
|
|||||||
conn.setRequestProperty("Range", "bytes=" + 0 + "-");
|
conn.setRequestProperty("Range", "bytes=" + 0 + "-");
|
||||||
conn.setConnectTimeout(mConnectTimeOut * 4);
|
conn.setConnectTimeout(mConnectTimeOut * 4);
|
||||||
conn.connect();
|
conn.connect();
|
||||||
|
handleConnect(conn);
|
||||||
|
} catch (IOException e) {
|
||||||
|
failDownload("下载失败【downloadUrl:"
|
||||||
|
+ mDownloadEntity.getDownloadUrl()
|
||||||
|
+ "】\n【filePath:"
|
||||||
|
+ mDownloadFile.getPath()
|
||||||
|
+ "】"
|
||||||
|
+ CommonUtil.getPrintException(e));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 处理状态吗
|
||||||
|
*/
|
||||||
|
private void handleConnect(HttpURLConnection conn) throws IOException {
|
||||||
int len = conn.getContentLength();
|
int len = conn.getContentLength();
|
||||||
//if (len < 0) { //网络被劫持时会出现这个问题
|
//if (len < 0) { //网络被劫持时会出现这个问题
|
||||||
// failDownload("下载失败,网络被劫持");
|
// failDownload("下载失败,网络被劫持");
|
||||||
@ -216,15 +231,13 @@ final class DownloadUtil implements IDownloadUtil, Runnable {
|
|||||||
//https://zh.wikipedia.org/wiki/HTTP%E7%8A%B6%E6%80%81%E7%A0%81
|
//https://zh.wikipedia.org/wiki/HTTP%E7%8A%B6%E6%80%81%E7%A0%81
|
||||||
//206支持断点
|
//206支持断点
|
||||||
if (code == HttpURLConnection.HTTP_PARTIAL) {
|
if (code == HttpURLConnection.HTTP_PARTIAL) {
|
||||||
|
if (!checkLen(len)) return;
|
||||||
isSupportBreakpoint = true;
|
isSupportBreakpoint = true;
|
||||||
mListener.supportBreakpoint(true);
|
mListener.supportBreakpoint(true);
|
||||||
handleBreakpoint(conn);
|
handleBreakpoint(conn);
|
||||||
} else if (code == HttpURLConnection.HTTP_OK || len < 0) {
|
} else if (code == HttpURLConnection.HTTP_OK) {
|
||||||
//在conn.setRequestProperty("Range", "bytes=" + 0 + "-");下,200为不支持断点状态
|
//在conn.setRequestProperty("Range", "bytes=" + 0 + "-");下,200为不支持断点状态
|
||||||
if (len < 0) {
|
if (!checkLen(len)) return;
|
||||||
failDownload("任务【" + mDownloadEntity.getDownloadUrl() + "】下载失败,文件长度小于0");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
isSupportBreakpoint = false;
|
isSupportBreakpoint = false;
|
||||||
mListener.supportBreakpoint(false);
|
mListener.supportBreakpoint(false);
|
||||||
Log.w(TAG, "该下载链接不支持断点下载");
|
Log.w(TAG, "该下载链接不支持断点下载");
|
||||||
@ -232,17 +245,47 @@ final class DownloadUtil implements IDownloadUtil, Runnable {
|
|||||||
} else if (code == HttpURLConnection.HTTP_NOT_FOUND) {
|
} else if (code == HttpURLConnection.HTTP_NOT_FOUND) {
|
||||||
Log.w(TAG, "任务【" + mDownloadEntity.getDownloadUrl() + "】下载失败,错误码:404");
|
Log.w(TAG, "任务【" + mDownloadEntity.getDownloadUrl() + "】下载失败,错误码:404");
|
||||||
mListener.onCancel();
|
mListener.onCancel();
|
||||||
|
} else if (code == HttpURLConnection.HTTP_MOVED_TEMP
|
||||||
|
|| code == HttpURLConnection.HTTP_MOVED_PERM
|
||||||
|
|| code == HttpURLConnection.HTTP_SEE_OTHER) {
|
||||||
|
handle302Turn(conn);
|
||||||
} else {
|
} else {
|
||||||
failDownload("任务【" + mDownloadEntity.getDownloadUrl() + "】下载失败,错误码:" + code);
|
failDownload("任务【" + mDownloadEntity.getDownloadUrl() + "】下载失败,错误码:" + code);
|
||||||
}
|
}
|
||||||
} catch (IOException e) {
|
|
||||||
failDownload("下载失败【downloadUrl:"
|
|
||||||
+ mDownloadEntity.getDownloadUrl()
|
|
||||||
+ "】\n【filePath:"
|
|
||||||
+ mDownloadFile.getPath()
|
|
||||||
+ "】"
|
|
||||||
+ CommonUtil.getPrintException(e));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 检查长度是否合法
|
||||||
|
*
|
||||||
|
* @param len 从服务器获取的文件长度
|
||||||
|
* @return true, 合法
|
||||||
|
*/
|
||||||
|
private boolean checkLen(long len) {
|
||||||
|
if (len < 0) {
|
||||||
|
failDownload("任务【" + mDownloadEntity.getDownloadUrl() + "】下载失败,文件长度小于0");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 处理30x跳转
|
||||||
|
*/
|
||||||
|
private void handle302Turn(HttpURLConnection conn) throws IOException {
|
||||||
|
String newUrl = conn.getHeaderField(mDownloadTaskEntity.redirectUrlKey);
|
||||||
|
Log.d(TAG, "30x跳转,新url为【" + newUrl + "】");
|
||||||
|
mDownloadEntity.setRedirect(true);
|
||||||
|
mDownloadEntity.setRedirectUrl(newUrl);
|
||||||
|
mDownloadEntity.update();
|
||||||
|
String cookies = conn.getHeaderField("Set-Cookie");
|
||||||
|
conn = (HttpURLConnection) new URL(newUrl).openConnection();
|
||||||
|
conn = ConnectionHelp.setConnectParam(mDownloadTaskEntity, conn);
|
||||||
|
conn.setRequestProperty("Cookie", cookies);
|
||||||
|
conn.setRequestProperty("Range", "bytes=" + 0 + "-");
|
||||||
|
conn.setConnectTimeout(mConnectTimeOut * 4);
|
||||||
|
conn.connect();
|
||||||
|
|
||||||
|
handleConnect(conn);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -307,7 +350,9 @@ final class DownloadUtil implements IDownloadUtil, Runnable {
|
|||||||
ConfigEntity entity = new ConfigEntity();
|
ConfigEntity entity = new ConfigEntity();
|
||||||
long len = conn.getContentLength();
|
long len = conn.getContentLength();
|
||||||
entity.FILE_SIZE = len;
|
entity.FILE_SIZE = len;
|
||||||
entity.DOWNLOAD_URL = mDownloadEntity.getDownloadUrl();
|
//entity.DOWNLOAD_URL = mDownloadEntity.getDownloadUrl();
|
||||||
|
entity.DOWNLOAD_URL = mDownloadEntity.isRedirect() ? mDownloadEntity.getRedirectUrl()
|
||||||
|
: mDownloadEntity.getDownloadUrl();
|
||||||
entity.TEMP_FILE = mDownloadFile;
|
entity.TEMP_FILE = mDownloadFile;
|
||||||
entity.THREAD_ID = 0;
|
entity.THREAD_ID = 0;
|
||||||
entity.START_LOCATION = 0;
|
entity.START_LOCATION = 0;
|
||||||
@ -383,7 +428,9 @@ final class DownloadUtil implements IDownloadUtil, Runnable {
|
|||||||
private void addSingleTask(int i, long startL, long endL, long fileLength) {
|
private void addSingleTask(int i, long startL, long endL, long fileLength) {
|
||||||
ConfigEntity entity = new ConfigEntity();
|
ConfigEntity entity = new ConfigEntity();
|
||||||
entity.FILE_SIZE = fileLength;
|
entity.FILE_SIZE = fileLength;
|
||||||
entity.DOWNLOAD_URL = mDownloadEntity.getDownloadUrl();
|
//entity.DOWNLOAD_URL = mDownloadEntity.getDownloadUrl();
|
||||||
|
entity.DOWNLOAD_URL = mDownloadEntity.isRedirect() ? mDownloadEntity.getRedirectUrl()
|
||||||
|
: mDownloadEntity.getDownloadUrl();
|
||||||
entity.TEMP_FILE = mDownloadFile;
|
entity.TEMP_FILE = mDownloadFile;
|
||||||
entity.THREAD_ID = i;
|
entity.THREAD_ID = i;
|
||||||
entity.START_LOCATION = startL;
|
entity.START_LOCATION = startL;
|
||||||
|
@ -15,6 +15,9 @@
|
|||||||
*/
|
*/
|
||||||
package com.arialyy.aria.core.download;
|
package com.arialyy.aria.core.download;
|
||||||
|
|
||||||
|
import android.os.Handler;
|
||||||
|
import android.os.Looper;
|
||||||
|
import android.os.Message;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
import com.arialyy.aria.util.BufferedRandomAccessFile;
|
import com.arialyy.aria.util.BufferedRandomAccessFile;
|
||||||
import com.arialyy.aria.util.CommonUtil;
|
import com.arialyy.aria.util.CommonUtil;
|
||||||
@ -33,6 +36,8 @@ import java.util.Properties;
|
|||||||
*/
|
*/
|
||||||
final class SingleThreadTask implements Runnable {
|
final class SingleThreadTask implements Runnable {
|
||||||
private static final String TAG = "SingleThreadTask";
|
private static final String TAG = "SingleThreadTask";
|
||||||
|
// TODO: 2017/2/22 不能使用1024 否则最大速度不能超过3m
|
||||||
|
private static final int BUF_SIZE = 8192;
|
||||||
private DownloadUtil.ConfigEntity mConfigEntity;
|
private DownloadUtil.ConfigEntity mConfigEntity;
|
||||||
private String mConfigFPath;
|
private String mConfigFPath;
|
||||||
private long mChildCurrentLocation = 0;
|
private long mChildCurrentLocation = 0;
|
||||||
@ -181,9 +186,33 @@ final class SingleThreadTask implements Runnable {
|
|||||||
mChildCurrentLocation += len;
|
mChildCurrentLocation += len;
|
||||||
mConstance.CURRENT_LOCATION += len;
|
mConstance.CURRENT_LOCATION += len;
|
||||||
mListener.onProgress(mConstance.CURRENT_LOCATION);
|
mListener.onProgress(mConstance.CURRENT_LOCATION);
|
||||||
|
//mHandler.sendEmptyMessage(1);
|
||||||
|
//mHandler.post(t);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Handler mHandler = new Handler(Looper.getMainLooper()) {
|
||||||
|
@Override public void handleMessage(Message msg) {
|
||||||
|
super.handleMessage(msg);
|
||||||
|
mListener.onProgress(mConstance.CURRENT_LOCATION);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
Thread t = new Thread(new Runnable() {
|
||||||
|
@Override public void run() {
|
||||||
|
mListener.onProgress(mConstance.CURRENT_LOCATION);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
//Handler handler = new Handler(){
|
||||||
|
// @Override public void handleMessage(Message msg) {
|
||||||
|
// super.handleMessage(msg);
|
||||||
|
// mListener.onProgress(mConstance.CURRENT_LOCATION);
|
||||||
|
// }
|
||||||
|
//};
|
||||||
|
|
||||||
|
Thread thread = new Thread();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 取消下载
|
* 取消下载
|
||||||
*/
|
*/
|
||||||
|
@ -16,6 +16,8 @@
|
|||||||
package com.arialyy.aria.core.inf;
|
package com.arialyy.aria.core.inf;
|
||||||
|
|
||||||
import android.support.annotation.NonNull;
|
import android.support.annotation.NonNull;
|
||||||
|
import android.text.TextUtils;
|
||||||
|
import android.util.Log;
|
||||||
import com.arialyy.aria.core.AriaManager;
|
import com.arialyy.aria.core.AriaManager;
|
||||||
import com.arialyy.aria.core.RequestEnum;
|
import com.arialyy.aria.core.RequestEnum;
|
||||||
import com.arialyy.aria.core.command.AbsCmd;
|
import com.arialyy.aria.core.command.AbsCmd;
|
||||||
@ -37,6 +39,17 @@ public class AbsTarget<ENTITY extends IEntity, TASK_ENTITY extends ITaskEntity>
|
|||||||
protected TASK_ENTITY taskEntity;
|
protected TASK_ENTITY taskEntity;
|
||||||
protected String targetName;
|
protected String targetName;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 重定向后,新url的key,默认为location
|
||||||
|
*/
|
||||||
|
protected void _setRedirectUrlKey(String redirectUrlKey) {
|
||||||
|
if (TextUtils.isEmpty(redirectUrlKey)) {
|
||||||
|
Log.w("AbsTarget", "重定向后,新url的key不能为null");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
taskEntity.redirectUrlKey = redirectUrlKey;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取任务文件大小
|
* 获取任务文件大小
|
||||||
*
|
*
|
||||||
|
@ -34,5 +34,10 @@ public abstract class ITaskEntity {
|
|||||||
*/
|
*/
|
||||||
public RequestEnum requestEnum = RequestEnum.GET;
|
public RequestEnum requestEnum = RequestEnum.GET;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 重定向后,新url的key
|
||||||
|
*/
|
||||||
|
public String redirectUrlKey = "location";
|
||||||
|
|
||||||
public abstract IEntity getEntity();
|
public abstract IEntity getEntity();
|
||||||
}
|
}
|
||||||
|
@ -95,7 +95,6 @@ public class DownloadSchedulers implements ISchedulers<DownloadTask> {
|
|||||||
startNextTask();
|
startNextTask();
|
||||||
break;
|
break;
|
||||||
case FAIL:
|
case FAIL:
|
||||||
//mQueue.removeTask(entity);
|
|
||||||
handleFailTask(task);
|
handleFailTask(task);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
32
Aria/src/main/java/com/arialyy/aria/orm/DBMapping.java
Normal file
32
Aria/src/main/java/com/arialyy/aria/orm/DBMapping.java
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2016 AriaLyy(https://github.com/AriaLyy/Aria)
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
package com.arialyy.aria.orm;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Created by Aria.Lao on 2017/4/6.
|
||||||
|
* DB映射表
|
||||||
|
*/
|
||||||
|
public class DBMapping {
|
||||||
|
static Map<String, String> mapping = new HashMap<>();
|
||||||
|
|
||||||
|
static {
|
||||||
|
mapping.put("DownloadEntity", "com.arialyy.aria.core.download.DownloadEntity");
|
||||||
|
mapping.put("UploadEntity", "com.arialyy.aria.core.upload.UploadEntity");
|
||||||
|
}
|
||||||
|
}
|
@ -21,6 +21,7 @@ import android.content.Context;
|
|||||||
import android.database.Cursor;
|
import android.database.Cursor;
|
||||||
import android.database.sqlite.SQLiteDatabase;
|
import android.database.sqlite.SQLiteDatabase;
|
||||||
import android.support.annotation.NonNull;
|
import android.support.annotation.NonNull;
|
||||||
|
import android.text.TextUtils;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
import com.arialyy.aria.util.CheckUtil;
|
import com.arialyy.aria.util.CheckUtil;
|
||||||
import com.arialyy.aria.util.CommonUtil;
|
import com.arialyy.aria.util.CommonUtil;
|
||||||
@ -37,12 +38,12 @@ public class DbUtil {
|
|||||||
private static final String TAG = "DbUtil";
|
private static final String TAG = "DbUtil";
|
||||||
private static final Object LOCK = new Object();
|
private static final Object LOCK = new Object();
|
||||||
private volatile static DbUtil INSTANCE = null;
|
private volatile static DbUtil INSTANCE = null;
|
||||||
private int CREATE_TABLE = 0;
|
private static final int CREATE_TABLE = 0;
|
||||||
private int TABLE_EXISTS = 1;
|
private static final int TABLE_EXISTS = 1;
|
||||||
private int INSERT_DATA = 2;
|
private static final int INSERT_DATA = 2;
|
||||||
private int MODIFY_DATA = 3;
|
private static final int MODIFY_DATA = 3;
|
||||||
private int FIND_DATA = 4;
|
private static final int FIND_DATA = 4;
|
||||||
private int FIND_ALL_DATA = 5;
|
private static final int FIND_ALL_DATA = 5;
|
||||||
private int DEL_DATA = 6;
|
private int DEL_DATA = 6;
|
||||||
private int ROW_ID = 7;
|
private int ROW_ID = 7;
|
||||||
private SQLiteDatabase mDb;
|
private SQLiteDatabase mDb;
|
||||||
@ -53,6 +54,11 @@ public class DbUtil {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private DbUtil(Context context) {
|
private DbUtil(Context context) {
|
||||||
|
//mHelper = new SqlHelper(context.getApplicationContext(), new SqlHelper.UpgradeListener() {
|
||||||
|
// @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
|
||||||
|
//
|
||||||
|
// }
|
||||||
|
//});
|
||||||
mHelper = new SqlHelper(context.getApplicationContext());
|
mHelper = new SqlHelper(context.getApplicationContext());
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -136,9 +142,10 @@ public class DbUtil {
|
|||||||
}
|
}
|
||||||
sb.append(i > 0 ? ", " : "");
|
sb.append(i > 0 ? ", " : "");
|
||||||
try {
|
try {
|
||||||
|
Object value = field.get(dbEntity);
|
||||||
sb.append(field.getName())
|
sb.append(field.getName())
|
||||||
.append("='")
|
.append("='")
|
||||||
.append(field.get(dbEntity).toString())
|
.append(value == null ? "" : value.toString())
|
||||||
.append("'");
|
.append("'");
|
||||||
} catch (IllegalAccessException e) {
|
} catch (IllegalAccessException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
@ -156,15 +163,24 @@ public class DbUtil {
|
|||||||
* 遍历所有数据
|
* 遍历所有数据
|
||||||
*/
|
*/
|
||||||
synchronized <T extends DbEntity> List<T> findAllData(Class<T> clazz) {
|
synchronized <T extends DbEntity> List<T> findAllData(Class<T> clazz) {
|
||||||
if (!tableExists(clazz)) {
|
if (mDb == null || !mDb.isOpen()) {
|
||||||
createTable(clazz);
|
|
||||||
}
|
|
||||||
mDb = mHelper.getReadableDatabase();
|
mDb = mHelper.getReadableDatabase();
|
||||||
|
}
|
||||||
|
return findAllData(mDb, clazz);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 遍历所有数据
|
||||||
|
*/
|
||||||
|
static synchronized <T extends DbEntity> List<T> findAllData(SQLiteDatabase db, Class<T> clazz) {
|
||||||
|
if (!tableExists(db, clazz)) {
|
||||||
|
createTable(db, clazz, null);
|
||||||
|
}
|
||||||
StringBuilder sb = new StringBuilder();
|
StringBuilder sb = new StringBuilder();
|
||||||
sb.append("SELECT rowid, * FROM ").append(CommonUtil.getClassName(clazz));
|
sb.append("SELECT rowid, * FROM ").append(CommonUtil.getClassName(clazz));
|
||||||
print(FIND_ALL_DATA, sb.toString());
|
print(FIND_ALL_DATA, sb.toString());
|
||||||
Cursor cursor = mDb.rawQuery(sb.toString(), null);
|
Cursor cursor = db.rawQuery(sb.toString(), null);
|
||||||
return cursor.getCount() > 0 ? newInstanceEntity(clazz, cursor) : null;
|
return cursor.getCount() > 0 ? newInstanceEntity(db, clazz, cursor) : null;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -186,7 +202,7 @@ public class DbUtil {
|
|||||||
sql = String.format(sql, params);
|
sql = String.format(sql, params);
|
||||||
print(FIND_DATA, sql);
|
print(FIND_DATA, sql);
|
||||||
Cursor cursor = mDb.rawQuery(sql, null);
|
Cursor cursor = mDb.rawQuery(sql, null);
|
||||||
return cursor.getCount() > 0 ? newInstanceEntity(clazz, cursor) : null;
|
return cursor.getCount() > 0 ? newInstanceEntity(mDb, clazz, cursor) : null;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -215,18 +231,17 @@ public class DbUtil {
|
|||||||
}
|
}
|
||||||
print(FIND_DATA, sb.toString());
|
print(FIND_DATA, sb.toString());
|
||||||
Cursor cursor = mDb.rawQuery(sb.toString(), null);
|
Cursor cursor = mDb.rawQuery(sb.toString(), null);
|
||||||
return cursor.getCount() > 0 ? newInstanceEntity(clazz, cursor) : null;
|
return cursor.getCount() > 0 ? newInstanceEntity(mDb, clazz, cursor) : null;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 插入数据
|
* 插入数据
|
||||||
*/
|
*/
|
||||||
synchronized void insertData(DbEntity dbEntity) {
|
static synchronized void insertData(SQLiteDatabase db, DbEntity dbEntity) {
|
||||||
Class<?> clazz = dbEntity.getClass();
|
Class<?> clazz = dbEntity.getClass();
|
||||||
if (!tableExists(clazz)) {
|
if (!tableExists(db, clazz)) {
|
||||||
createTable(clazz);
|
createTable(db, clazz, null);
|
||||||
}
|
}
|
||||||
mDb = mHelper.getWritableDatabase();
|
|
||||||
Field[] fields = CommonUtil.getFields(clazz);
|
Field[] fields = CommonUtil.getFields(clazz);
|
||||||
if (fields != null && fields.length > 0) {
|
if (fields != null && fields.length > 0) {
|
||||||
StringBuilder sb = new StringBuilder();
|
StringBuilder sb = new StringBuilder();
|
||||||
@ -259,8 +274,18 @@ public class DbUtil {
|
|||||||
}
|
}
|
||||||
sb.append(")");
|
sb.append(")");
|
||||||
print(INSERT_DATA, sb.toString());
|
print(INSERT_DATA, sb.toString());
|
||||||
mDb.execSQL(sb.toString());
|
db.execSQL(sb.toString());
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 插入数据
|
||||||
|
*/
|
||||||
|
synchronized void insertData(DbEntity dbEntity) {
|
||||||
|
if (mDb == null || !mDb.isOpen()) {
|
||||||
|
mDb = mHelper.getReadableDatabase();
|
||||||
|
}
|
||||||
|
insertData(mDb, dbEntity);
|
||||||
close();
|
close();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -271,6 +296,10 @@ public class DbUtil {
|
|||||||
if (mDb == null || !mDb.isOpen()) {
|
if (mDb == null || !mDb.isOpen()) {
|
||||||
mDb = mHelper.getReadableDatabase();
|
mDb = mHelper.getReadableDatabase();
|
||||||
}
|
}
|
||||||
|
return tableExists(mDb, clazz);
|
||||||
|
}
|
||||||
|
|
||||||
|
static synchronized boolean tableExists(SQLiteDatabase db, Class clazz) {
|
||||||
Cursor cursor = null;
|
Cursor cursor = null;
|
||||||
try {
|
try {
|
||||||
StringBuilder sb = new StringBuilder();
|
StringBuilder sb = new StringBuilder();
|
||||||
@ -278,7 +307,7 @@ public class DbUtil {
|
|||||||
sb.append(CommonUtil.getClassName(clazz));
|
sb.append(CommonUtil.getClassName(clazz));
|
||||||
sb.append("'");
|
sb.append("'");
|
||||||
print(TABLE_EXISTS, sb.toString());
|
print(TABLE_EXISTS, sb.toString());
|
||||||
cursor = mDb.rawQuery(sb.toString(), null);
|
cursor = db.rawQuery(sb.toString(), null);
|
||||||
if (cursor != null && cursor.moveToNext()) {
|
if (cursor != null && cursor.moveToNext()) {
|
||||||
int count = cursor.getInt(0);
|
int count = cursor.getInt(0);
|
||||||
if (count > 0) {
|
if (count > 0) {
|
||||||
@ -289,22 +318,20 @@ public class DbUtil {
|
|||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
} finally {
|
} finally {
|
||||||
if (cursor != null) cursor.close();
|
if (cursor != null) cursor.close();
|
||||||
close();
|
if (db != null) {
|
||||||
|
db.close();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
static synchronized void createTable(SQLiteDatabase db, Class clazz, String tableName) {
|
||||||
* 创建表
|
|
||||||
*/
|
|
||||||
private synchronized void createTable(Class clazz) {
|
|
||||||
if (mDb == null || !mDb.isOpen()) {
|
|
||||||
mDb = mHelper.getWritableDatabase();
|
|
||||||
}
|
|
||||||
Field[] fields = CommonUtil.getFields(clazz);
|
Field[] fields = CommonUtil.getFields(clazz);
|
||||||
if (fields != null && fields.length > 0) {
|
if (fields != null && fields.length > 0) {
|
||||||
StringBuilder sb = new StringBuilder();
|
StringBuilder sb = new StringBuilder();
|
||||||
sb.append("create table ").append(CommonUtil.getClassName(clazz)).append("(");
|
sb.append("create table ")
|
||||||
|
.append(TextUtils.isEmpty(tableName) ? CommonUtil.getClassName(clazz) : tableName)
|
||||||
|
.append("(");
|
||||||
for (Field field : fields) {
|
for (Field field : fields) {
|
||||||
field.setAccessible(true);
|
field.setAccessible(true);
|
||||||
if (ignoreField(field)) {
|
if (ignoreField(field)) {
|
||||||
@ -336,15 +363,31 @@ public class DbUtil {
|
|||||||
String str = sb.toString();
|
String str = sb.toString();
|
||||||
str = str.substring(0, str.length() - 1) + ");";
|
str = str.substring(0, str.length() - 1) + ");";
|
||||||
print(CREATE_TABLE, str);
|
print(CREATE_TABLE, str);
|
||||||
mDb.execSQL(str);
|
db.execSQL(str);
|
||||||
}
|
}
|
||||||
close();
|
if (db != null) {
|
||||||
|
db.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
synchronized void createTable(Class clazz, String tableName) {
|
||||||
|
if (mDb == null || !mDb.isOpen()) {
|
||||||
|
mDb = mHelper.getWritableDatabase();
|
||||||
|
}
|
||||||
|
createTable(mDb, clazz, tableName);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 创建表
|
||||||
|
*/
|
||||||
|
private synchronized void createTable(Class clazz) {
|
||||||
|
createTable(clazz, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return true 忽略该字段
|
* @return true 忽略该字段
|
||||||
*/
|
*/
|
||||||
private boolean ignoreField(Field field) {
|
static boolean ignoreField(Field field) {
|
||||||
// field.isSynthetic(), 使用as热启动App时,AS会自动给你的clss添加change字段
|
// field.isSynthetic(), 使用as热启动App时,AS会自动给你的clss添加change字段
|
||||||
Ignore ignore = field.getAnnotation(Ignore.class);
|
Ignore ignore = field.getAnnotation(Ignore.class);
|
||||||
return (ignore != null && ignore.value()) || field.isSynthetic();
|
return (ignore != null && ignore.value()) || field.isSynthetic();
|
||||||
@ -355,28 +398,28 @@ public class DbUtil {
|
|||||||
*
|
*
|
||||||
* @param type {@link DbUtil}
|
* @param type {@link DbUtil}
|
||||||
*/
|
*/
|
||||||
private void print(int type, String sql) {
|
private static void print(int type, String sql) {
|
||||||
if (true) {
|
if (true) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
String str = "";
|
String str = "";
|
||||||
switch (type) {
|
switch (type) {
|
||||||
case 0:
|
case CREATE_TABLE:
|
||||||
str = "创建表 >>>> ";
|
str = "创建表 >>>> ";
|
||||||
break;
|
break;
|
||||||
case 1:
|
case TABLE_EXISTS:
|
||||||
str = "表是否存在 >>>> ";
|
str = "表是否存在 >>>> ";
|
||||||
break;
|
break;
|
||||||
case 2:
|
case INSERT_DATA:
|
||||||
str = "插入数据 >>>> ";
|
str = "插入数据 >>>> ";
|
||||||
break;
|
break;
|
||||||
case 3:
|
case MODIFY_DATA:
|
||||||
str = "修改数据 >>>> ";
|
str = "修改数据 >>>> ";
|
||||||
break;
|
break;
|
||||||
case 4:
|
case FIND_DATA:
|
||||||
str = "查询一行数据 >>>> ";
|
str = "查询一行数据 >>>> ";
|
||||||
break;
|
break;
|
||||||
case 5:
|
case FIND_ALL_DATA:
|
||||||
str = "遍历整个数据库 >>>> ";
|
str = "遍历整个数据库 >>>> ";
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@ -440,8 +483,8 @@ public class DbUtil {
|
|||||||
/**
|
/**
|
||||||
* 根据数据游标创建一个具体的对象
|
* 根据数据游标创建一个具体的对象
|
||||||
*/
|
*/
|
||||||
private synchronized <T extends DbEntity> List<T> newInstanceEntity(Class<T> clazz,
|
private static synchronized <T extends DbEntity> List<T> newInstanceEntity(SQLiteDatabase db,
|
||||||
Cursor cursor) {
|
Class<T> clazz, Cursor cursor) {
|
||||||
Field[] fields = CommonUtil.getFields(clazz);
|
Field[] fields = CommonUtil.getFields(clazz);
|
||||||
List<T> entitys = new ArrayList<>();
|
List<T> entitys = new ArrayList<>();
|
||||||
if (fields != null && fields.length > 0) {
|
if (fields != null && fields.length > 0) {
|
||||||
@ -486,7 +529,10 @@ public class DbUtil {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
cursor.close();
|
cursor.close();
|
||||||
close();
|
//close();
|
||||||
|
if (db != null) {
|
||||||
|
db.close();
|
||||||
|
}
|
||||||
return entitys;
|
return entitys;
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -17,17 +17,29 @@
|
|||||||
package com.arialyy.aria.orm;
|
package com.arialyy.aria.orm;
|
||||||
|
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
|
import android.database.Cursor;
|
||||||
import android.database.sqlite.SQLiteDatabase;
|
import android.database.sqlite.SQLiteDatabase;
|
||||||
import android.database.sqlite.SQLiteOpenHelper;
|
import android.database.sqlite.SQLiteOpenHelper;
|
||||||
import android.text.TextUtils;
|
import android.text.TextUtils;
|
||||||
|
import android.util.Log;
|
||||||
|
import com.arialyy.aria.util.CommonUtil;
|
||||||
|
import java.io.File;
|
||||||
|
import java.lang.reflect.Field;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Created by lyy on 2015/11/2.
|
* Created by lyy on 2015/11/2.
|
||||||
* sql帮助类
|
* sql帮助类
|
||||||
*/
|
*/
|
||||||
final class SqlHelper extends SQLiteOpenHelper {
|
final class SqlHelper extends SQLiteOpenHelper {
|
||||||
protected static String DB_NAME;
|
interface UpgradeListener {
|
||||||
protected static int VERSION = -1;
|
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion);
|
||||||
|
}
|
||||||
|
|
||||||
|
private UpgradeListener mUpgradeListener;
|
||||||
|
static String DB_NAME;
|
||||||
|
static int VERSION = -1;
|
||||||
|
|
||||||
static {
|
static {
|
||||||
if (TextUtils.isEmpty(DB_NAME)) {
|
if (TextUtils.isEmpty(DB_NAME)) {
|
||||||
@ -38,7 +50,12 @@ final class SqlHelper extends SQLiteOpenHelper {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public SqlHelper(Context context) {
|
//SqlHelper(Context context, UpgradeListener listener) {
|
||||||
|
// super(context, DB_NAME, null, VERSION);
|
||||||
|
// mUpgradeListener = listener;
|
||||||
|
//}
|
||||||
|
|
||||||
|
SqlHelper(Context context) {
|
||||||
super(context, DB_NAME, null, VERSION);
|
super(context, DB_NAME, null, VERSION);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -47,6 +64,82 @@ final class SqlHelper extends SQLiteOpenHelper {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
|
@Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
|
||||||
|
try {
|
||||||
|
if (oldVersion < newVersion) {
|
||||||
|
handleDbUpdate(db);
|
||||||
|
}
|
||||||
|
} catch (ClassNotFoundException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 处理数据库升级
|
||||||
|
*
|
||||||
|
* @throws ClassNotFoundException
|
||||||
|
*/
|
||||||
|
private void handleDbUpdate(SQLiteDatabase db) throws ClassNotFoundException {
|
||||||
|
if (db == null) {
|
||||||
|
Log.d("SqlHelper", "db 为 null");
|
||||||
|
return;
|
||||||
|
} else if (!db.isOpen()) {
|
||||||
|
Log.d("SqlHelper", "db已关闭");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
Set<String> tables = DBMapping.mapping.keySet();
|
||||||
|
for (String tableName : tables) {
|
||||||
|
Class clazz = Class.forName(DBMapping.mapping.get(tableName));
|
||||||
|
if (DbUtil.tableExists(db, clazz)) {
|
||||||
|
String countColumnSql = "SELECT rowid FROM " + tableName;
|
||||||
|
Cursor cursor = db.rawQuery(countColumnSql, null);
|
||||||
|
int dbColumnNum = cursor.getColumnCount();
|
||||||
|
int newEntityColumnNum = getEntityAttr(clazz);
|
||||||
|
if (dbColumnNum != newEntityColumnNum) {
|
||||||
|
back(db, clazz);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 备份
|
||||||
|
*/
|
||||||
|
private void back(SQLiteDatabase db, Class clazz) {
|
||||||
|
String oldTableName = CommonUtil.getClassName(clazz);
|
||||||
|
//备份数据
|
||||||
|
List<DbEntity> list = DbUtil.findAllData(db, clazz);
|
||||||
|
//修改原来表名字
|
||||||
|
String alertSql = "alter table " + oldTableName + " rename to " + oldTableName + "_temp";
|
||||||
|
db.beginTransaction();
|
||||||
|
db.execSQL(alertSql);
|
||||||
|
//创建一个原来新表
|
||||||
|
DbUtil.createTable(db, clazz, null);
|
||||||
|
for (DbEntity entity : list) {
|
||||||
|
DbUtil.insertData(db, entity);
|
||||||
|
}
|
||||||
|
//删除原来的表
|
||||||
|
String deleteSQL = "drop table IF EXISTS " + oldTableName + "_temp";
|
||||||
|
db.execSQL(deleteSQL);
|
||||||
|
db.setTransactionSuccessful();
|
||||||
|
db.endTransaction();
|
||||||
|
db.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取实体的字段数
|
||||||
|
*/
|
||||||
|
private int getEntityAttr(Class clazz) {
|
||||||
|
int count = 1;
|
||||||
|
Field[] fields = CommonUtil.getFields(clazz);
|
||||||
|
if (fields != null && fields.length > 0) {
|
||||||
|
for (Field field : fields) {
|
||||||
|
field.setAccessible(true);
|
||||||
|
if (DbUtil.ignoreField(field)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
count++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return count;
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -141,6 +141,7 @@ compile 'com.arialyy.aria:Aria:3.0.0'
|
|||||||
有任何问题,可以在[issues](https://github.com/AriaLyy/Aria/issues)给我留言反馈。
|
有任何问题,可以在[issues](https://github.com/AriaLyy/Aria/issues)给我留言反馈。
|
||||||
|
|
||||||
***
|
***
|
||||||
|
|
||||||
## 开发日志
|
## 开发日志
|
||||||
+ v_3.0.0 添加上传任务支持,修复一些已发现的bug
|
+ v_3.0.0 添加上传任务支持,修复一些已发现的bug
|
||||||
+ v_2.4.4 修复不支持断点的下载链接拿不到文件大小的问题
|
+ v_2.4.4 修复不支持断点的下载链接拿不到文件大小的问题
|
||||||
|
@ -2,7 +2,7 @@ apply plugin: 'com.android.application'
|
|||||||
|
|
||||||
android {
|
android {
|
||||||
compileSdkVersion 23
|
compileSdkVersion 23
|
||||||
buildToolsVersion "23.0.2"
|
buildToolsVersion '25.0.2'
|
||||||
|
|
||||||
defaultConfig {
|
defaultConfig {
|
||||||
applicationId "com.arialyy.simple"
|
applicationId "com.arialyy.simple"
|
||||||
|
17
app/proguard-rules.pro
vendored
17
app/proguard-rules.pro
vendored
@ -1,17 +0,0 @@
|
|||||||
# Add project specific ProGuard rules here.
|
|
||||||
# By default, the flags in this file are appended to flags specified
|
|
||||||
# in D:\sdk/tools/proguard/proguard-android.txt
|
|
||||||
# You can edit the include path and order by changing the proguardFiles
|
|
||||||
# directive in build.gradle.
|
|
||||||
#
|
|
||||||
# For more details, see
|
|
||||||
# http://developer.android.com/guide/developing/tools/proguard.html
|
|
||||||
|
|
||||||
# Add any project specific keep options here:
|
|
||||||
|
|
||||||
# If your project uses WebView with JS, uncomment the following
|
|
||||||
# and specify the fully qualified class name to the JavaScript interface
|
|
||||||
# class:
|
|
||||||
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
|
|
||||||
# public *;
|
|
||||||
#}
|
|
@ -57,7 +57,8 @@ public class SingleTaskActivity extends BaseActivity<ActivitySingleBinding> {
|
|||||||
//"https://atom-installer.github.com/v1.13.0/AtomSetup.exe?s=1484074138&ext=.exe";
|
//"https://atom-installer.github.com/v1.13.0/AtomSetup.exe?s=1484074138&ext=.exe";
|
||||||
//"http://static.gaoshouyou.com/d/22/94/822260b849944492caadd2983f9bb624.apk";
|
//"http://static.gaoshouyou.com/d/22/94/822260b849944492caadd2983f9bb624.apk";
|
||||||
//不支持断点的链接
|
//不支持断点的链接
|
||||||
"http://ox.konsung.net:5555/ksdc-web/download/downloadFile/?fileName=ksdc_1.0.2.apk&rRange=0-";
|
//"http://ox.konsung.net:5555/ksdc-web/download/downloadFile/?fileName=ksdc_1.0.2.apk&rRange=0-";
|
||||||
|
"http://172.18.104.50:8080/download/_302turn";
|
||||||
@Bind(R.id.progressBar) HorizontalProgressBarWithNumber mPb;
|
@Bind(R.id.progressBar) HorizontalProgressBarWithNumber mPb;
|
||||||
@Bind(R.id.start) Button mStart;
|
@Bind(R.id.start) Button mStart;
|
||||||
@Bind(R.id.stop) Button mStop;
|
@Bind(R.id.stop) Button mStop;
|
||||||
|
@ -25,7 +25,7 @@
|
|||||||
|
|
||||||
</string-array>
|
</string-array>
|
||||||
<string-array name="download_url">
|
<string-array name="download_url">
|
||||||
<item>http://g37.gdl.netease.com/onmyoji_netease.apk</item>
|
<item>http://g37.gdl.netease.com/onmyoji_netease_1.apk</item>
|
||||||
<item>http://static.gaoshouyou.com/d/eb/f2/dfeba30541f209ab8a50d847fc1661ce.apk</item>
|
<item>http://static.gaoshouyou.com/d/eb/f2/dfeba30541f209ab8a50d847fc1661ce.apk</item>
|
||||||
<item>http://rs.0.gaoshouyou.com/d/51/46/58514d126c46b8a3f27fc8c7db3b09ec.apk</item>
|
<item>http://rs.0.gaoshouyou.com/d/51/46/58514d126c46b8a3f27fc8c7db3b09ec.apk</item>
|
||||||
<item>http://rs.0.gaoshouyou.com/d/23/69/07238f952669727878d7a0e180534c8b.apk</item>
|
<item>http://rs.0.gaoshouyou.com/d/23/69/07238f952669727878d7a0e180534c8b.apk</item>
|
||||||
|
2
gradle/wrapper/gradle-wrapper.properties
vendored
2
gradle/wrapper/gradle-wrapper.properties
vendored
@ -1,4 +1,4 @@
|
|||||||
#Mon Oct 24 17:59:34 CST 2016
|
#Tue Mar 14 19:13:43 CST 2017
|
||||||
distributionBase=GRADLE_USER_HOME
|
distributionBase=GRADLE_USER_HOME
|
||||||
distributionPath=wrapper/dists
|
distributionPath=wrapper/dists
|
||||||
zipStoreBase=GRADLE_USER_HOME
|
zipStoreBase=GRADLE_USER_HOME
|
||||||
|
Binary file not shown.
Before Width: | Height: | Size: 44 KiB After Width: | Height: | Size: 47 KiB |
Reference in New Issue
Block a user