Permalink
Browse files

优化配置文件的读取

  • Loading branch information...
AriaLyy committed Nov 29, 2018
1 parent 9fc5f47 commit bd1b3e7416f3a5bb90f364f0bf7693567286046c
@@ -130,7 +130,7 @@ private void amendTaskState() {
DownloadEntity.class, UploadEntity.class, DownloadGroupEntity.class,
DownloadTaskEntity.class, UploadTaskEntity.class, DownloadGroupTaskEntity.class
};
String sql = "UPDATE %s SET state=2 WHERE state in (3,4,5,6)";
String sql = "UPDATE %s SET state=2 WHERE state IN (3,4,5,6)";
for (Class clazz : clazzs) {
String temp = String.format(sql, clazz.getSimpleName());
DbEntity.exeSql(temp);
@@ -376,6 +376,10 @@ private String createKey(@ReceiverType String type, Object obj) {
* 初始化配置文件
*/
private void initConfig() {
mDConfig = Configuration.getInstance().downloadCfg;
mUConfig = Configuration.getInstance().uploadCfg;
mAConfig = Configuration.getInstance().appCfg;
File xmlFile = new File(APP.getFilesDir().getPath() + Configuration.XML_FILE);
File tempDir = new File(APP.getFilesDir().getPath() + "/temp");
if (!xmlFile.exists()) {
@@ -389,16 +393,13 @@ private void initConfig() {
}
CommonUtil.createFileFormInputStream(APP.getAssets().open("aria_config.xml"),
file.getPath());
if (!CommonUtil.checkMD5(md5Code, file)) {
if (!CommonUtil.checkMD5(md5Code, file) || !Configuration.getInstance().configExists()) {
loadConfig();
}
} catch (IOException e) {
e.printStackTrace();
}
}
mDConfig = Configuration.DownloadConfig.getInstance();
mUConfig = Configuration.UploadConfig.getInstance();
mAConfig = Configuration.AppConfig.getInstance();
if (tempDir.exists()) {
File newDir = new File(APP.getFilesDir().getPath() + DOWNLOAD_TEMP_DIR);
newDir.mkdirs();
@@ -22,15 +22,14 @@
import org.xml.sax.helpers.DefaultHandler;
/**
* Created by lyy on 2017/5/22.
* 读取配置文件
* Created by lyy on 2017/5/22. 读取配置文件
*/
class ConfigHelper extends DefaultHandler {
private final String TAG = "ConfigHelper";
private Configuration.DownloadConfig mDownloadConfig = Configuration.DownloadConfig.getInstance();
private Configuration.UploadConfig mUploadConfig = Configuration.UploadConfig.getInstance();
private Configuration.AppConfig mAppConfig = Configuration.AppConfig.getInstance();
private Configuration.DownloadConfig mDownloadConfig = Configuration.getInstance().downloadCfg;
private Configuration.UploadConfig mUploadConfig = Configuration.getInstance().uploadCfg;
private Configuration.AppConfig mAppConfig = Configuration.getInstance().appCfg;
private @ConfigType int mType;
@Override public void startDocument() throws SAXException {
@@ -357,8 +356,8 @@ private boolean checkBoolean(String value) {
@Override public void endDocument() throws SAXException {
super.endDocument();
mDownloadConfig.saveAll();
mUploadConfig.saveAll();
mAppConfig.saveAll();
mDownloadConfig.save();
mUploadConfig.save();
mAppConfig.save();
}
}
@@ -57,13 +57,7 @@ protected void saveData(int state, long location) {
mEntity.setComplete(state == IEntity.STATE_COMPLETE);
if (state == IEntity.STATE_CANCEL) {
if (mEntity instanceof DownloadEntity) {
TaskRecord record =
DbEntity.findFirst(TaskRecord.class, "TaskRecord.filePath=?", mTaskEntity.getKey());
if (record != null) {
CommonUtil.delTaskRecord(record, mTaskEntity.isRemoveFile(), mEntity);
} else {
CommonUtil.delTaskRecord(mEntity.getDownloadPath(), 1, mTaskEntity.isRemoveFile());
}
CommonUtil.delTaskRecord(mEntity.getDownloadPath(), 1, mTaskEntity.isRemoveFile());
}
return;
} else if (state == IEntity.STATE_STOP) {
@@ -42,17 +42,8 @@ protected void saveData(int state, long location) {
mEntity.setComplete(state == IEntity.STATE_COMPLETE);
if (state == IEntity.STATE_CANCEL) {
if (mEntity instanceof UploadEntity) {
TaskRecord record =
DbEntity.findFirst(TaskRecord.class, "TaskRecord.filePath=?", mTaskEntity.getKey());
if (record != null) {
CommonUtil.delTaskRecord(record, mTaskEntity.isRemoveFile(), mEntity);
}else {
CommonUtil.delTaskRecord(mEntity.getFilePath(), 2, mTaskEntity.isRemoveFile());
}
CommonUtil.delTaskRecord(mEntity.getFilePath(), 2, mTaskEntity.isRemoveFile());
}
//else if (mEntity instanceof AbsGroupEntity) {
// CommonUtil.delGroupTaskRecord(mTaskEntity.isRemoveFile(), ((AbsGroupEntity) mEntity));
//}
return;
} else if (state == IEntity.STATE_STOP) {
mEntity.setStopTime(System.currentTimeMillis());
@@ -50,6 +50,9 @@
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.Field;
import java.lang.reflect.ParameterizedType;
@@ -76,6 +79,102 @@
public class CommonUtil {
private static final String TAG = "CommonUtil";
/**
* 删除文件
* @param path 文件路径
* @return {@code true}删除成功、{@co'de false}删除失败
*/
public static boolean deleteFile(String path){
if (TextUtils.isEmpty(path)){
ALog.e(TAG, "删除文件失败,路径为空");
return false;
}
File file = new File(path);
if (file.exists()) {
final File to = new File(file.getAbsolutePath() + System.currentTimeMillis());
if (file.renameTo(to)) {
return to.delete();
} else {
return file.delete();
}
}
return false;
}
/**
* 将对象写入文件
*
* @param filePath 文件路径
* @param data data数据必须实现{@link Serializable}接口
*/
public static void writeObjToFile(String filePath, Object data) {
if (!(data instanceof Serializable)) {
ALog.e(TAG, "对象写入文件失败,data数据必须实现Serializable接口");
return;
}
FileOutputStream ops = null;
try {
if (!createFile(filePath)) {
return;
}
ops = new FileOutputStream(filePath);
ObjectOutputStream oops = new ObjectOutputStream(ops);
oops.writeObject(data);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (ops != null) {
try {
ops.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
/**
* 从文件中读取对象
*
* @param filePath 文件路径
* @return 如果读取成功,返回相应的Obj对象,读取失败,返回null
*/
public static Object readObjFromFile(String filePath) {
if (TextUtils.isEmpty(filePath)) {
ALog.e(TAG, "文件路径为空");
return null;
}
File file = new File(filePath);
if (!file.exists()) {
ALog.e(TAG, String.format("文件【%s】不存在", filePath));
return null;
}
FileInputStream fis = null;
try {
fis = new FileInputStream(filePath);
ObjectInputStream oois = new ObjectInputStream(fis);
Object obj = oois.readObject();
return obj;
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} finally {
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return null;
}
/**
* 获取分块文件的快大小
*
@@ -583,7 +682,7 @@ public static void delTaskRecord(String filePath, int type, boolean removeFile)
TaskRecord record = DbEntity.findFirst(TaskRecord.class, "filePath=?", filePath);
File file = new File(filePath);
if (record == null) {
ALog.w(TAG, "删除记录失败,记录为空");
ALog.w(TAG, "记录为空");
} else {
// 删除分块文件
if (record.isBlock) {
@@ -991,11 +1090,13 @@ public static boolean createDir(String path) {
/**
* 创建文件 当文件不存在的时候就创建一个文件。 如果文件存在,先删除原文件,然后重新创建一个新文件
*
* @return {@code true} 创建成功、{@code false} 创建失败
*/
public static void createFile(String path) {
public static boolean createFile(String path) {
if (TextUtils.isEmpty(path)) {
ALog.e(TAG, "文件路径不能为null");
return;
return false;
}
File file = new File(path);
if (file.getParentFile() == null || !file.getParentFile().exists()) {
@@ -1016,10 +1117,13 @@ public static void createFile(String path) {
try {
if (file.createNewFile()) {
ALog.d(TAG, "创建文件成功:" + file.getAbsolutePath());
return true;
}
} catch (IOException e) {
e.printStackTrace();
return false;
}
return false;
}
/**
@@ -19,6 +19,7 @@
- 初始化时自动判断文件是否被删除,文件被删除的任务将自动重置默认值
- 修复刷新url后,文件无法删除的 bug
- fix bug https://github.com/AriaLyy/Aria/issues/309
- 优化配置文件的读取
+ v_3.5.1
- 优化`taskExists`方法
- 添加`post`参数请求支持
@@ -13,7 +13,7 @@
<download>
<!--设置任务最大下载速度,0表示不限速,单位为:kb-->
<maxSpeed value="0"/>
<maxSpeed value="128"/>
<!--
多线程下载是否使用块下载模式,{@code true}使用,{@code false}不使用
@@ -47,11 +47,11 @@
public class SingleTaskActivity extends BaseActivity<ActivitySingleBinding> {
private static final String DOWNLOAD_URL =
private String DOWNLOAD_URL =
//"http://kotlinlang.org/docs/kotlin-docs.pdf";
//"https://atom-installer.github.com/v1.13.0/AtomSetup.exe?s=1484074138&ext=.exe";
//"http://static.gaoshouyou.com/d/22/94/822260b849944492caadd2983f9bb624.apks";
"http://120.55.95.61:8811/ghcg/zg/武义总规纲要成果.zip";
"http://120.55.95.61:8811/ghcg/zg/武义总规纲要成果.zips";
//"https://yizi-kejian.oss-cn-beijing.aliyuncs.com/qimeng/package1/qmtable11.zip";
//"http://rs.0.gaoshouyou.com/d/04/1e/400423a7551e1f3f0eb1812afa1f9b44.apk";
//"http://58.210.9.131/tpk/sipgt//TDLYZTGH.tpk"; //chunked 下载
@@ -77,8 +77,8 @@
* 设置start 和 stop 按钮状态
*/
private void setBtState(boolean state) {
mStart.setEnabled(state);
mStop.setEnabled(!state);
//mStart.setEnabled(state);
//mStop.setEnabled(!state);
}
@Override public boolean onCreateOptionsMenu(Menu menu) {
@@ -141,7 +141,7 @@ private void setBtState(boolean state) {
@Download.onTaskRunning protected void running(DownloadTask task) {
ALog.d(TAG, String.format("%s_running_%s", getClass().getName(), hashCode()));
if (task.getKey().equals(DOWNLOAD_URL)) {
//if (task.getKey().equals(DOWNLOAD_URL)) {
//Log.d(TAG, task.getKey());
long len = task.getFileSize();
if (len == 0) {
@@ -150,7 +150,7 @@ private void setBtState(boolean state) {
getBinding().setProgress(task.getPercent());
}
getBinding().setSpeed(task.getConvertSpeed());
}
//}
}
@Download.onTaskResume void taskResume(DownloadTask task) {
@@ -187,16 +187,17 @@ private void setBtState(boolean state) {
.load(DOWNLOAD_URL)
.updateUrl("http://120.55.95.61:8811/ghcg/zg/武义总规纲要成果.zip")
.start();
//DOWNLOAD_URL = "http://120.55.95.61:8811/ghcg/zg/武义总规纲要成果.zip";
//ALog.d(TAG, ALog.getExceptionString(e));
}
}
@Download.onTaskComplete void taskComplete(DownloadTask task) {
if (task.getKey().equals(DOWNLOAD_URL)) {
//if (task.getKey().equals(DOWNLOAD_URL)) {
getBinding().setProgress(100);
Toast.makeText(SingleTaskActivity.this, "下载完成", Toast.LENGTH_SHORT).show();
mStart.setText("重新开始?");
mCancel.setEnabled(false);
//mCancel.setEnabled(false);
setBtState(true);
getBinding().setSpeed("");
L.d(TAG, "path ==> " + task.getDownloadEntity().getDownloadPath());
@@ -207,7 +208,7 @@ private void setBtState(boolean state) {
//File apkFile = new File(task.getDownloadPath());
//install.setDataAndType(Uri.fromFile(apkFile), "application/vnd.android.package-archive");
//startActivity(install);
}
//}
}
@Download.onNoSupportBreakPoint public void onNoSupportBreakPoint(DownloadTask task) {
@@ -96,7 +96,7 @@
@Upload.onTaskComplete public void taskComplete(UploadTask task) {
L.d(TAG, "上传完成");
L.d(TAG, "上传成功返回数据(如果有的话):" + task.getEntity().getResponseStr());
L.d(TAG, "上传成功服务端返回数据(如果有的话):" + task.getEntity().getResponseStr());
getBinding().setSpeed("");
getBinding().setProgress(100);
}
Oops, something went wrong.

0 comments on commit bd1b3e7

Please sign in to comment.