添加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 {
|
||||
compileSdkVersion 23
|
||||
buildToolsVersion "23.0.2"
|
||||
buildToolsVersion '25.0.2'
|
||||
|
||||
defaultConfig {
|
||||
minSdkVersion 9
|
||||
|
@ -27,7 +27,6 @@ import android.content.Context;
|
||||
import android.os.Build;
|
||||
import android.widget.PopupWindow;
|
||||
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.ISchedulerListener;
|
||||
import com.arialyy.aria.core.download.DownloadTask;
|
||||
@ -207,8 +206,8 @@ import com.arialyy.aria.core.upload.UploadTask;
|
||||
/**
|
||||
* 下载任务状态监听
|
||||
*/
|
||||
public static class DownloadSchedulerListener implements
|
||||
IDownloadSchedulerListener<DownloadTask> {
|
||||
public static class DownloadSchedulerListener
|
||||
implements IDownloadSchedulerListener<DownloadTask> {
|
||||
|
||||
@Override public void onTaskPre(DownloadTask task) {
|
||||
|
||||
|
@ -257,7 +257,10 @@ import java.util.Map;
|
||||
} else if (obj instanceof Application) {
|
||||
key = clsName;
|
||||
}
|
||||
} else {
|
||||
}
|
||||
if (obj instanceof Activity || obj instanceof Service) {
|
||||
key = clsName;
|
||||
} else if (obj instanceof Application) {
|
||||
key = clsName;
|
||||
}
|
||||
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, */*");
|
||||
////用于处理Disconnect 不起作用问题
|
||||
//conn.setRequestProperty("Connection", "close");
|
||||
conn.setRequestProperty("Connection", "Keep-Alive");
|
||||
//302获取重定向地址
|
||||
conn.setInstanceFollowRedirects(false);
|
||||
return conn;
|
||||
}
|
||||
}
|
||||
|
@ -30,15 +30,6 @@ import com.arialyy.aria.orm.DbEntity;
|
||||
* !!!并且需要Parcelable时需要手动填写rowID;
|
||||
*/
|
||||
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 int failNum = 0;
|
||||
private String downloadUrl = ""; //下载路径
|
||||
@ -50,25 +41,12 @@ public class DownloadEntity extends DbEntity implements Parcelable, IEntity {
|
||||
private boolean isDownloadComplete = false; //是否下载完成
|
||||
private long currentProgress = 0; //当前下载进度
|
||||
private long completeTime; //完成时间
|
||||
private boolean isRedirect = false;
|
||||
private String redirectUrl = ""; //重定向链接
|
||||
|
||||
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() {
|
||||
return str;
|
||||
}
|
||||
@ -164,16 +142,40 @@ public class DownloadEntity extends DbEntity implements Parcelable, IEntity {
|
||||
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() {
|
||||
return "DownloadEntity{"
|
||||
+ "downloadUrl='"
|
||||
+ "speed="
|
||||
+ speed
|
||||
+ ", failNum="
|
||||
+ failNum
|
||||
+ ", downloadUrl='"
|
||||
+ downloadUrl
|
||||
+ '\''
|
||||
+ ", downloadPath='"
|
||||
+ downloadPath
|
||||
+ '\''
|
||||
+ ", completeTime="
|
||||
+ completeTime
|
||||
+ ", fileName='"
|
||||
+ fileName
|
||||
+ '\''
|
||||
+ ", str='"
|
||||
+ str
|
||||
+ '\''
|
||||
+ ", fileSize="
|
||||
+ fileSize
|
||||
+ ", state="
|
||||
@ -182,8 +184,13 @@ public class DownloadEntity extends DbEntity implements Parcelable, IEntity {
|
||||
+ isDownloadComplete
|
||||
+ ", currentProgress="
|
||||
+ currentProgress
|
||||
+ ", failNum="
|
||||
+ failNum
|
||||
+ ", completeTime="
|
||||
+ 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) {
|
||||
dest.writeLong(this.speed);
|
||||
dest.writeInt(this.failNum);
|
||||
dest.writeString(this.downloadUrl);
|
||||
dest.writeString(this.downloadPath);
|
||||
dest.writeString(this.fileName);
|
||||
dest.writeString(this.str);
|
||||
dest.writeLong(this.completeTime);
|
||||
dest.writeLong(this.fileSize);
|
||||
dest.writeInt(this.state);
|
||||
dest.writeByte(this.isDownloadComplete ? (byte) 1 : (byte) 0);
|
||||
dest.writeLong(this.currentProgress);
|
||||
dest.writeInt(this.failNum);
|
||||
dest.writeLong(this.speed);
|
||||
dest.writeInt(this.rowID);
|
||||
dest.writeLong(this.completeTime);
|
||||
dest.writeByte(this.isRedirect ? (byte) 1 : (byte) 0);
|
||||
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.text.TextUtils;
|
||||
import android.util.Log;
|
||||
import com.arialyy.aria.core.RequestEnum;
|
||||
import com.arialyy.aria.core.inf.AbsTarget;
|
||||
import com.arialyy.aria.core.queue.DownloadTaskQueue;
|
||||
@ -44,6 +45,13 @@ public class DownloadTarget extends AbsTarget<DownloadEntity, DownloadTaskEntity
|
||||
super.resume();
|
||||
}
|
||||
|
||||
/**
|
||||
* 重定向后,新url的key,默认为location
|
||||
*/
|
||||
public void setRedirectUrlKey(String redirectUrlKey) {
|
||||
super._setRedirectUrlKey(redirectUrlKey);
|
||||
}
|
||||
|
||||
/**
|
||||
* 给url请求添加头部
|
||||
*
|
||||
|
@ -246,8 +246,10 @@ public class DownloadTask implements ITask {
|
||||
|
||||
@Override public void supportBreakpoint(boolean support) {
|
||||
super.supportBreakpoint(support);
|
||||
sendInState2Target(ISchedulers.SUPPORT_BREAK_POINT);
|
||||
sendIntent(Aria.ACTION_SUPPORT_BREAK_POINT, -1);
|
||||
if (!support) {
|
||||
sendInState2Target(ISchedulers.SUPPORT_BREAK_POINT);
|
||||
sendIntent(Aria.ACTION_SUPPORT_BREAK_POINT, -1);
|
||||
}
|
||||
}
|
||||
|
||||
@Override public void onPre() {
|
||||
|
@ -34,7 +34,7 @@ import java.util.concurrent.Executors;
|
||||
* 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";
|
||||
/**
|
||||
* 线程数
|
||||
@ -207,34 +207,7 @@ final class DownloadUtil implements IDownloadUtil, Runnable {
|
||||
conn.setRequestProperty("Range", "bytes=" + 0 + "-");
|
||||
conn.setConnectTimeout(mConnectTimeOut * 4);
|
||||
conn.connect();
|
||||
int len = conn.getContentLength();
|
||||
//if (len < 0) { //网络被劫持时会出现这个问题
|
||||
// failDownload("下载失败,网络被劫持");
|
||||
// return;
|
||||
//}
|
||||
int code = conn.getResponseCode();
|
||||
//https://zh.wikipedia.org/wiki/HTTP%E7%8A%B6%E6%80%81%E7%A0%81
|
||||
//206支持断点
|
||||
if (code == HttpURLConnection.HTTP_PARTIAL) {
|
||||
isSupportBreakpoint = true;
|
||||
mListener.supportBreakpoint(true);
|
||||
handleBreakpoint(conn);
|
||||
} else if (code == HttpURLConnection.HTTP_OK || len < 0) {
|
||||
//在conn.setRequestProperty("Range", "bytes=" + 0 + "-");下,200为不支持断点状态
|
||||
if (len < 0) {
|
||||
failDownload("任务【" + mDownloadEntity.getDownloadUrl() + "】下载失败,文件长度小于0");
|
||||
return;
|
||||
}
|
||||
isSupportBreakpoint = false;
|
||||
mListener.supportBreakpoint(false);
|
||||
Log.w(TAG, "该下载链接不支持断点下载");
|
||||
handleBreakpoint(conn);
|
||||
} else if (code == HttpURLConnection.HTTP_NOT_FOUND) {
|
||||
Log.w(TAG, "任务【" + mDownloadEntity.getDownloadUrl() + "】下载失败,错误码:404");
|
||||
mListener.onCancel();
|
||||
} else {
|
||||
failDownload("任务【" + mDownloadEntity.getDownloadUrl() + "】下载失败,错误码:" + code);
|
||||
}
|
||||
handleConnect(conn);
|
||||
} catch (IOException e) {
|
||||
failDownload("下载失败【downloadUrl:"
|
||||
+ mDownloadEntity.getDownloadUrl()
|
||||
@ -245,6 +218,76 @@ final class DownloadUtil implements IDownloadUtil, Runnable {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理状态吗
|
||||
*/
|
||||
private void handleConnect(HttpURLConnection conn) throws IOException {
|
||||
int len = conn.getContentLength();
|
||||
//if (len < 0) { //网络被劫持时会出现这个问题
|
||||
// failDownload("下载失败,网络被劫持");
|
||||
// return;
|
||||
//}
|
||||
int code = conn.getResponseCode();
|
||||
//https://zh.wikipedia.org/wiki/HTTP%E7%8A%B6%E6%80%81%E7%A0%81
|
||||
//206支持断点
|
||||
if (code == HttpURLConnection.HTTP_PARTIAL) {
|
||||
if (!checkLen(len)) return;
|
||||
isSupportBreakpoint = true;
|
||||
mListener.supportBreakpoint(true);
|
||||
handleBreakpoint(conn);
|
||||
} else if (code == HttpURLConnection.HTTP_OK) {
|
||||
//在conn.setRequestProperty("Range", "bytes=" + 0 + "-");下,200为不支持断点状态
|
||||
if (!checkLen(len)) return;
|
||||
isSupportBreakpoint = false;
|
||||
mListener.supportBreakpoint(false);
|
||||
Log.w(TAG, "该下载链接不支持断点下载");
|
||||
handleBreakpoint(conn);
|
||||
} else if (code == HttpURLConnection.HTTP_NOT_FOUND) {
|
||||
Log.w(TAG, "任务【" + mDownloadEntity.getDownloadUrl() + "】下载失败,错误码:404");
|
||||
mListener.onCancel();
|
||||
} else if (code == HttpURLConnection.HTTP_MOVED_TEMP
|
||||
|| code == HttpURLConnection.HTTP_MOVED_PERM
|
||||
|| code == HttpURLConnection.HTTP_SEE_OTHER) {
|
||||
handle302Turn(conn);
|
||||
} else {
|
||||
failDownload("任务【" + mDownloadEntity.getDownloadUrl() + "】下载失败,错误码:" + code);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查长度是否合法
|
||||
*
|
||||
* @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();
|
||||
long len = conn.getContentLength();
|
||||
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.THREAD_ID = 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) {
|
||||
ConfigEntity entity = new ConfigEntity();
|
||||
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.THREAD_ID = i;
|
||||
entity.START_LOCATION = startL;
|
||||
|
@ -15,6 +15,9 @@
|
||||
*/
|
||||
package com.arialyy.aria.core.download;
|
||||
|
||||
import android.os.Handler;
|
||||
import android.os.Looper;
|
||||
import android.os.Message;
|
||||
import android.util.Log;
|
||||
import com.arialyy.aria.util.BufferedRandomAccessFile;
|
||||
import com.arialyy.aria.util.CommonUtil;
|
||||
@ -32,7 +35,9 @@ import java.util.Properties;
|
||||
* 下载线程
|
||||
*/
|
||||
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 String mConfigFPath;
|
||||
private long mChildCurrentLocation = 0;
|
||||
@ -181,9 +186,33 @@ final class SingleThreadTask implements Runnable {
|
||||
mChildCurrentLocation += len;
|
||||
mConstance.CURRENT_LOCATION += len;
|
||||
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;
|
||||
|
||||
import android.support.annotation.NonNull;
|
||||
import android.text.TextUtils;
|
||||
import android.util.Log;
|
||||
import com.arialyy.aria.core.AriaManager;
|
||||
import com.arialyy.aria.core.RequestEnum;
|
||||
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 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;
|
||||
|
||||
/**
|
||||
* 重定向后,新url的key
|
||||
*/
|
||||
public String redirectUrlKey = "location";
|
||||
|
||||
public abstract IEntity getEntity();
|
||||
}
|
||||
|
@ -95,7 +95,6 @@ public class DownloadSchedulers implements ISchedulers<DownloadTask> {
|
||||
startNextTask();
|
||||
break;
|
||||
case FAIL:
|
||||
//mQueue.removeTask(entity);
|
||||
handleFailTask(task);
|
||||
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.sqlite.SQLiteDatabase;
|
||||
import android.support.annotation.NonNull;
|
||||
import android.text.TextUtils;
|
||||
import android.util.Log;
|
||||
import com.arialyy.aria.util.CheckUtil;
|
||||
import com.arialyy.aria.util.CommonUtil;
|
||||
@ -37,12 +38,12 @@ public class DbUtil {
|
||||
private static final String TAG = "DbUtil";
|
||||
private static final Object LOCK = new Object();
|
||||
private volatile static DbUtil INSTANCE = null;
|
||||
private int CREATE_TABLE = 0;
|
||||
private int TABLE_EXISTS = 1;
|
||||
private int INSERT_DATA = 2;
|
||||
private int MODIFY_DATA = 3;
|
||||
private int FIND_DATA = 4;
|
||||
private int FIND_ALL_DATA = 5;
|
||||
private static final int CREATE_TABLE = 0;
|
||||
private static final int TABLE_EXISTS = 1;
|
||||
private static final int INSERT_DATA = 2;
|
||||
private static final int MODIFY_DATA = 3;
|
||||
private static final int FIND_DATA = 4;
|
||||
private static final int FIND_ALL_DATA = 5;
|
||||
private int DEL_DATA = 6;
|
||||
private int ROW_ID = 7;
|
||||
private SQLiteDatabase mDb;
|
||||
@ -53,6 +54,11 @@ public class DbUtil {
|
||||
}
|
||||
|
||||
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());
|
||||
}
|
||||
|
||||
@ -136,9 +142,10 @@ public class DbUtil {
|
||||
}
|
||||
sb.append(i > 0 ? ", " : "");
|
||||
try {
|
||||
Object value = field.get(dbEntity);
|
||||
sb.append(field.getName())
|
||||
.append("='")
|
||||
.append(field.get(dbEntity).toString())
|
||||
.append(value == null ? "" : value.toString())
|
||||
.append("'");
|
||||
} catch (IllegalAccessException e) {
|
||||
e.printStackTrace();
|
||||
@ -156,15 +163,24 @@ public class DbUtil {
|
||||
* 遍历所有数据
|
||||
*/
|
||||
synchronized <T extends DbEntity> List<T> findAllData(Class<T> clazz) {
|
||||
if (!tableExists(clazz)) {
|
||||
createTable(clazz);
|
||||
if (mDb == null || !mDb.isOpen()) {
|
||||
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);
|
||||
}
|
||||
mDb = mHelper.getReadableDatabase();
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.append("SELECT rowid, * FROM ").append(CommonUtil.getClassName(clazz));
|
||||
print(FIND_ALL_DATA, sb.toString());
|
||||
Cursor cursor = mDb.rawQuery(sb.toString(), null);
|
||||
return cursor.getCount() > 0 ? newInstanceEntity(clazz, cursor) : null;
|
||||
Cursor cursor = db.rawQuery(sb.toString(), null);
|
||||
return cursor.getCount() > 0 ? newInstanceEntity(db, clazz, cursor) : null;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -186,7 +202,7 @@ public class DbUtil {
|
||||
sql = String.format(sql, params);
|
||||
print(FIND_DATA, sql);
|
||||
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());
|
||||
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();
|
||||
if (!tableExists(clazz)) {
|
||||
createTable(clazz);
|
||||
if (!tableExists(db, clazz)) {
|
||||
createTable(db, clazz, null);
|
||||
}
|
||||
mDb = mHelper.getWritableDatabase();
|
||||
Field[] fields = CommonUtil.getFields(clazz);
|
||||
if (fields != null && fields.length > 0) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
@ -259,8 +274,18 @@ public class DbUtil {
|
||||
}
|
||||
sb.append(")");
|
||||
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();
|
||||
}
|
||||
|
||||
@ -271,6 +296,10 @@ public class DbUtil {
|
||||
if (mDb == null || !mDb.isOpen()) {
|
||||
mDb = mHelper.getReadableDatabase();
|
||||
}
|
||||
return tableExists(mDb, clazz);
|
||||
}
|
||||
|
||||
static synchronized boolean tableExists(SQLiteDatabase db, Class clazz) {
|
||||
Cursor cursor = null;
|
||||
try {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
@ -278,7 +307,7 @@ public class DbUtil {
|
||||
sb.append(CommonUtil.getClassName(clazz));
|
||||
sb.append("'");
|
||||
print(TABLE_EXISTS, sb.toString());
|
||||
cursor = mDb.rawQuery(sb.toString(), null);
|
||||
cursor = db.rawQuery(sb.toString(), null);
|
||||
if (cursor != null && cursor.moveToNext()) {
|
||||
int count = cursor.getInt(0);
|
||||
if (count > 0) {
|
||||
@ -289,22 +318,20 @@ public class DbUtil {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
if (cursor != null) cursor.close();
|
||||
close();
|
||||
if (db != null) {
|
||||
db.close();
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建表
|
||||
*/
|
||||
private synchronized void createTable(Class clazz) {
|
||||
if (mDb == null || !mDb.isOpen()) {
|
||||
mDb = mHelper.getWritableDatabase();
|
||||
}
|
||||
static synchronized void createTable(SQLiteDatabase db, Class clazz, String tableName) {
|
||||
Field[] fields = CommonUtil.getFields(clazz);
|
||||
if (fields != null && fields.length > 0) {
|
||||
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) {
|
||||
field.setAccessible(true);
|
||||
if (ignoreField(field)) {
|
||||
@ -336,15 +363,31 @@ public class DbUtil {
|
||||
String str = sb.toString();
|
||||
str = str.substring(0, str.length() - 1) + ");";
|
||||
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 忽略该字段
|
||||
*/
|
||||
private boolean ignoreField(Field field) {
|
||||
static boolean ignoreField(Field field) {
|
||||
// field.isSynthetic(), 使用as热启动App时,AS会自动给你的clss添加change字段
|
||||
Ignore ignore = field.getAnnotation(Ignore.class);
|
||||
return (ignore != null && ignore.value()) || field.isSynthetic();
|
||||
@ -355,28 +398,28 @@ public class DbUtil {
|
||||
*
|
||||
* @param type {@link DbUtil}
|
||||
*/
|
||||
private void print(int type, String sql) {
|
||||
private static void print(int type, String sql) {
|
||||
if (true) {
|
||||
return;
|
||||
}
|
||||
String str = "";
|
||||
switch (type) {
|
||||
case 0:
|
||||
case CREATE_TABLE:
|
||||
str = "创建表 >>>> ";
|
||||
break;
|
||||
case 1:
|
||||
case TABLE_EXISTS:
|
||||
str = "表是否存在 >>>> ";
|
||||
break;
|
||||
case 2:
|
||||
case INSERT_DATA:
|
||||
str = "插入数据 >>>> ";
|
||||
break;
|
||||
case 3:
|
||||
case MODIFY_DATA:
|
||||
str = "修改数据 >>>> ";
|
||||
break;
|
||||
case 4:
|
||||
case FIND_DATA:
|
||||
str = "查询一行数据 >>>> ";
|
||||
break;
|
||||
case 5:
|
||||
case FIND_ALL_DATA:
|
||||
str = "遍历整个数据库 >>>> ";
|
||||
break;
|
||||
}
|
||||
@ -440,8 +483,8 @@ public class DbUtil {
|
||||
/**
|
||||
* 根据数据游标创建一个具体的对象
|
||||
*/
|
||||
private synchronized <T extends DbEntity> List<T> newInstanceEntity(Class<T> clazz,
|
||||
Cursor cursor) {
|
||||
private static synchronized <T extends DbEntity> List<T> newInstanceEntity(SQLiteDatabase db,
|
||||
Class<T> clazz, Cursor cursor) {
|
||||
Field[] fields = CommonUtil.getFields(clazz);
|
||||
List<T> entitys = new ArrayList<>();
|
||||
if (fields != null && fields.length > 0) {
|
||||
@ -486,7 +529,10 @@ public class DbUtil {
|
||||
}
|
||||
}
|
||||
cursor.close();
|
||||
close();
|
||||
//close();
|
||||
if (db != null) {
|
||||
db.close();
|
||||
}
|
||||
return entitys;
|
||||
}
|
||||
}
|
@ -17,17 +17,29 @@
|
||||
package com.arialyy.aria.orm;
|
||||
|
||||
import android.content.Context;
|
||||
import android.database.Cursor;
|
||||
import android.database.sqlite.SQLiteDatabase;
|
||||
import android.database.sqlite.SQLiteOpenHelper;
|
||||
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.
|
||||
* sql帮助类
|
||||
*/
|
||||
final class SqlHelper extends SQLiteOpenHelper {
|
||||
protected static String DB_NAME;
|
||||
protected static int VERSION = -1;
|
||||
interface UpgradeListener {
|
||||
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion);
|
||||
}
|
||||
|
||||
private UpgradeListener mUpgradeListener;
|
||||
static String DB_NAME;
|
||||
static int VERSION = -1;
|
||||
|
||||
static {
|
||||
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);
|
||||
}
|
||||
|
||||
@ -47,6 +64,82 @@ final class SqlHelper extends SQLiteOpenHelper {
|
||||
}
|
||||
|
||||
@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)给我留言反馈。
|
||||
|
||||
***
|
||||
|
||||
## 开发日志
|
||||
+ v_3.0.0 添加上传任务支持,修复一些已发现的bug
|
||||
+ v_2.4.4 修复不支持断点的下载链接拿不到文件大小的问题
|
||||
|
@ -2,7 +2,7 @@ apply plugin: 'com.android.application'
|
||||
|
||||
android {
|
||||
compileSdkVersion 23
|
||||
buildToolsVersion "23.0.2"
|
||||
buildToolsVersion '25.0.2'
|
||||
|
||||
defaultConfig {
|
||||
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";
|
||||
//"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.start) Button mStart;
|
||||
@Bind(R.id.stop) Button mStop;
|
||||
|
@ -25,7 +25,7 @@
|
||||
|
||||
</string-array>
|
||||
<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://rs.0.gaoshouyou.com/d/51/46/58514d126c46b8a3f27fc8c7db3b09ec.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
|
||||
distributionPath=wrapper/dists
|
||||
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