启动逻辑修改

pull/2/head
AriaLyy 8 years ago
parent 374fdd12b9
commit 6d696ccfa2
  1. 3
      app/src/main/java/com/arialyy/simple/activity/MainActivity.java
  2. 10
      downloadutil/src/main/java/com/arialyy/downloadutil/core/AMReceiver.java
  3. 35
      downloadutil/src/main/java/com/arialyy/downloadutil/core/Aria.java
  4. 20
      downloadutil/src/main/java/com/arialyy/downloadutil/core/AriaManager.java
  5. 4
      downloadutil/src/main/java/com/arialyy/downloadutil/core/DownloadManager.java
  6. 21
      downloadutil/src/main/java/com/arialyy/downloadutil/core/queue/DownloadTaskQueue.java
  7. 24
      downloadutil/src/main/java/com/arialyy/downloadutil/core/scheduler/DownloadSchedulers.java
  8. 2
      downloadutil/src/main/java/com/arialyy/downloadutil/core/scheduler/IDownloadSchedulers.java
  9. 20
      downloadutil/src/main/java/com/arialyy/downloadutil/core/task/Task.java
  10. 31
      downloadutil/src/main/java/com/arialyy/downloadutil/orm/DbUtil.java

@ -19,6 +19,7 @@ package com.arialyy.simple.activity;
import android.Manifest; import android.Manifest;
import android.content.Intent; import android.content.Intent;
import android.os.Build;
import android.os.Bundle; import android.os.Bundle;
import android.support.v7.widget.Toolbar; import android.support.v7.widget.Toolbar;
import android.view.View; import android.view.View;
@ -49,7 +50,7 @@ public class MainActivity extends BaseActivity<ActivityMainBinding> {
mBar.setTitle("多线程多任务下载"); mBar.setTitle("多线程多任务下载");
boolean hasPermission = PermissionManager.getInstance() boolean hasPermission = PermissionManager.getInstance()
.checkPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE); .checkPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE);
if (hasPermission) { if (hasPermission || Build.VERSION.SDK_INT <= Build.VERSION_CODES.M) {
setEnable(true); setEnable(true);
} else { } else {
setEnable(false); setEnable(false);

@ -16,19 +16,13 @@
package com.arialyy.downloadutil.core; package com.arialyy.downloadutil.core;
import android.content.Context; import android.content.Context;
import com.arialyy.downloadutil.core.command.CmdFactory;
import com.arialyy.downloadutil.core.command.IDownloadCmd;
import com.arialyy.downloadutil.core.scheduler.OnSchedulerListener; import com.arialyy.downloadutil.core.scheduler.OnSchedulerListener;
import com.arialyy.downloadutil.core.task.Task;
import com.arialyy.downloadutil.util.CommonUtil;
import java.util.ArrayList;
import java.util.List;
/** /**
* AM 接收器 * AM 接收器
*/ */
public class AMReceiver { public class AMReceiver {
Context context; Object obj;
OnSchedulerListener listener; OnSchedulerListener listener;
DownloadEntity entity; DownloadEntity entity;
DownloadManager manager = DownloadManager.getInstance(); DownloadManager manager = DownloadManager.getInstance();
@ -43,7 +37,7 @@ public class AMReceiver {
*/ */
public AMReceiver addSchedulerListener(OnSchedulerListener listener) { public AMReceiver addSchedulerListener(OnSchedulerListener listener) {
this.listener = listener; this.listener = listener;
manager.getTaskQueue().getDownloadSchedulers().addSchedulerListener(context, listener); manager.getTaskQueue().getDownloadSchedulers().addSchedulerListener(listener);
return this; return this;
} }

@ -17,28 +17,49 @@
package com.arialyy.downloadutil.core; package com.arialyy.downloadutil.core;
import android.annotation.TargetApi; import android.annotation.TargetApi;
import android.app.Activity;
import android.app.Application;
import android.app.Fragment;
import android.app.Service;
import android.content.Context; import android.content.Context;
import android.content.ContextWrapper;
import android.os.Build; import android.os.Build;
import com.bumptech.glide.Glide;
/** /**
* Created by lyy on 2016/12/1. * Created by lyy on 2016/12/1.
* Aria启动管理全局任务 * Aria启动管理全局任务
*/ */
@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1) @TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1) public class Aria {
public class Aria {
private static final Object LOCK = new Object();
private static volatile Aria INSTANCE = null;
private DownloadManager mDownloadManager;
private Aria() { private Aria() {
//mDownloadManager = DownloadManager.getInstance();
} }
public static AMReceiver whit(Context context) { public static AMReceiver whit(Context context) {
if (context == null) throw new IllegalArgumentException("context 不能为 null");
if (context instanceof Activity
|| context instanceof Service
|| context instanceof Application) {
return AriaManager.getInstance(context).get(context); return AriaManager.getInstance(context).get(context);
} else {
throw new IllegalArgumentException("这是不支持的context");
}
}
public static AMReceiver whit(Fragment fragment) {
return AriaManager.getInstance(
Build.VERSION.SDK_INT >= Build.VERSION_CODES.M ? fragment.getContext()
: fragment.getActivity()).get(fragment);
} }
public static AriaManager get(Context context){ public static AriaManager get(Context context) {
if (context == null) throw new IllegalArgumentException("context 不能为 null");
if (context instanceof Activity
|| context instanceof Service
|| context instanceof Application) {
return AriaManager.getInstance(context); return AriaManager.getInstance(context);
} else {
throw new IllegalArgumentException("这是不支持的context");
}
} }
} }

@ -41,8 +41,8 @@ import java.util.Set;
return INSTANCE; return INSTANCE;
} }
AMReceiver get(Context context) { AMReceiver get(Object obj) {
return getTarget(context); return getTarget(obj);
} }
/** /**
@ -77,21 +77,21 @@ import java.util.Set;
} }
} }
private AMReceiver putTarget(Context context) { private AMReceiver putTarget(Object obj) {
String clsName = context.getClass().getName(); String clsName = obj.getClass().getName();
AMReceiver target = mTargets.get(clsName); AMReceiver target = mTargets.get(clsName);
if (target == null) { if (target == null) {
target = new AMReceiver(); target = new AMReceiver();
target.context = context; target.obj = obj;
mTargets.put(clsName, target); mTargets.put(clsName, target);
} }
return target; return target;
} }
private AMReceiver getTarget(Context context) { private AMReceiver getTarget(Object obj) {
AMReceiver target = mTargets.get(context.getClass().getName()); AMReceiver target = mTargets.get(obj.getClass().getName());
if (target == null) { if (target == null) {
target = putTarget(context); target = putTarget(obj);
} }
return target; return target;
} }
@ -141,8 +141,8 @@ import java.util.Set;
for (String key : keys) { for (String key : keys) {
if (key.equals(activity.getClass().getName())) { if (key.equals(activity.getClass().getName())) {
AMReceiver target = mTargets.get(key); AMReceiver target = mTargets.get(key);
if (target.context != null) { if (target.obj != null) {
if (target.context instanceof Application || target.context instanceof Service) break; if (target.obj instanceof Application || target.obj instanceof Service) break;
target.removeSchedulerListener(); target.removeSchedulerListener();
mTargets.remove(key); mTargets.remove(key);
} }

@ -99,15 +99,11 @@ public class DownloadManager {
} }
public static DownloadManager init(Context context) { public static DownloadManager init(Context context) {
if (context instanceof Application) {
if (INSTANCE == null) { if (INSTANCE == null) {
synchronized (LOCK) { synchronized (LOCK) {
INSTANCE = new DownloadManager(context.getApplicationContext()); INSTANCE = new DownloadManager(context.getApplicationContext());
} }
} }
} else {
Log.e(TAG, "Context 只能为application");
}
return INSTANCE; return INSTANCE;
} }

@ -87,21 +87,24 @@ public class DownloadTaskQueue implements ITaskQueue {
} }
@Override public void stopTask(Task task) { @Override public void stopTask(Task task) {
if (task.isDownloading()) { if (!task.isDownloading())Log.w(TAG, "停止任务失败,【任务已经停止】");
if (mExecutePool.removeTask(task)) {
task.stop(); task.stop();
} //if (task.isDownloading()) {
} else { // if (mExecutePool.removeTask(task)) {
task.stop(); // task.stop();
Log.w(TAG, "停止任务失败,【任务已经停止】"); // }
} //} else {
// task.stop();
// Log.w(TAG, "停止任务失败,【任务已经停止】");
//}
} }
@Override public void cancelTask(Task task) { @Override public void cancelTask(Task task) {
if (mExecutePool.removeTask(task) || mCachePool.removeTask(task)) { //if (mExecutePool.removeTask(task) || mCachePool.removeTask(task)) {
// task.cancel();
//}
task.cancel(); task.cancel();
} }
}
@Override public void reTryStart(Task task) { @Override public void reTryStart(Task task) {
if (!task.isDownloading()) { if (!task.isDownloading()) {

@ -98,14 +98,17 @@ public class DownloadSchedulers implements IDownloadSchedulers {
} }
@Override public boolean handleMessage(Message msg) { @Override public boolean handleMessage(Message msg) {
DownloadEntity entity = (DownloadEntity) msg.obj; Task task = (Task) msg.obj;
if (entity == null) { if (task == null) {
Log.e(TAG, "请传入下载实体DownloadEntity"); Log.e(TAG, "请传入下载任务");
return true; return true;
} }
callback(msg.what, task);
DownloadEntity entity = task.getDownloadEntity();
switch (msg.what) { switch (msg.what) {
case STOP: case STOP:
case CANCEL: case CANCEL:
mQueue.removeTask(entity);
if (mQueue.size() != ExecutePool.mSize) { if (mQueue.size() != ExecutePool.mSize) {
startNextTask(entity); startNextTask(entity);
} }
@ -117,7 +120,6 @@ public class DownloadSchedulers implements IDownloadSchedulers {
handleFailTask(entity); handleFailTask(entity);
break; break;
} }
callback(msg.what, entity);
return true; return true;
} }
@ -125,22 +127,22 @@ public class DownloadSchedulers implements IDownloadSchedulers {
* 回调 * 回调
* *
* @param state 状态 * @param state 状态
* @param entity 下载实体
*/ */
private void callback(int state, DownloadEntity entity) { private void callback(int state, Task task) {
if (mSchedulerListeners.size() > 0) { if (mSchedulerListeners.size() > 0) {
//Set<Map.Entry<Integer, String>> //Set<Map.Entry<Integer, String>>
for (Map.Entry<Integer, OnSchedulerListener> entry : mSchedulerListeners.entrySet()) { for (Map.Entry<Integer, OnSchedulerListener> entry : mSchedulerListeners.entrySet()) {
callback(state, entity, entry.getValue()); callback(state, task, entry.getValue());
} }
} }
} }
private void callback(int state, DownloadEntity entity, OnSchedulerListener listener) { private void callback(int state, Task task, OnSchedulerListener listener) {
if (listener != null) { if (listener != null) {
Task task = mQueue.getTask(entity); //Task task = mQueue.getTask(entity);
if (task == null) { if (task == null) {
Log.e(TAG, "队列中没有下载链接【" + entity.getDownloadUrl() + "】的任务"); //Log.e(TAG, "队列中没有下载链接【" + entity.getDownloadUrl() + "】的任务");
Log.e(TAG, "传递的下载任务");
return; return;
} }
switch (state) { switch (state) {
@ -207,7 +209,7 @@ public class DownloadSchedulers implements IDownloadSchedulers {
} }
@Override @Override
public void addSchedulerListener(Context context, OnSchedulerListener schedulerListener) { public void addSchedulerListener(OnSchedulerListener schedulerListener) {
mSchedulerListeners.put(schedulerListener.hashCode(), schedulerListener); mSchedulerListeners.put(schedulerListener.hashCode(), schedulerListener);
} }

@ -31,7 +31,7 @@ public interface IDownloadSchedulers extends Handler.Callback {
* *
* @param schedulerListener {@link OnSchedulerListener} * @param schedulerListener {@link OnSchedulerListener}
*/ */
public void addSchedulerListener(Context context, OnSchedulerListener schedulerListener); public void addSchedulerListener(OnSchedulerListener schedulerListener);
/** /**
* 取消注册监听器 * 取消注册监听器

@ -141,7 +141,7 @@ public class Task {
*/ */
private void sendInState2Target(int state) { private void sendInState2Target(int state) {
if (mOutHandler != null) { if (mOutHandler != null) {
mOutHandler.obtainMessage(state, mEntity).sendToTarget(); mOutHandler.obtainMessage(state, this).sendToTarget();
} }
} }
@ -218,7 +218,7 @@ public class Task {
@Override public void onPre() { @Override public void onPre() {
super.onPre(); super.onPre();
downloadEntity.setState(DownloadEntity.STATE_PRE); downloadEntity.setState(DownloadEntity.STATE_PRE);
sendIntent(DownloadManager.ACTION_PRE, -1); //sendIntent(DownloadManager.ACTION_PRE, -1);
} }
@Override public void onPostPre(long fileSize) { @Override public void onPostPre(long fileSize) {
@ -226,21 +226,21 @@ public class Task {
downloadEntity.setFileSize(fileSize); downloadEntity.setFileSize(fileSize);
downloadEntity.setState(DownloadEntity.STATE_POST_PRE); downloadEntity.setState(DownloadEntity.STATE_POST_PRE);
sendInState2Target(DownloadSchedulers.PRE); sendInState2Target(DownloadSchedulers.PRE);
sendIntent(DownloadManager.ACTION_POST_PRE, -1); //sendIntent(DownloadManager.ACTION_POST_PRE, -1);
} }
@Override public void onResume(long resumeLocation) { @Override public void onResume(long resumeLocation) {
super.onResume(resumeLocation); super.onResume(resumeLocation);
downloadEntity.setState(DownloadEntity.STATE_DOWNLOAD_ING); downloadEntity.setState(DownloadEntity.STATE_DOWNLOAD_ING);
sendInState2Target(DownloadSchedulers.RESUME); sendInState2Target(DownloadSchedulers.RESUME);
sendIntent(DownloadManager.ACTION_RESUME, resumeLocation); //sendIntent(DownloadManager.ACTION_RESUME, resumeLocation);
} }
@Override public void onStart(long startLocation) { @Override public void onStart(long startLocation) {
super.onStart(startLocation); super.onStart(startLocation);
downloadEntity.setState(DownloadEntity.STATE_DOWNLOAD_ING); downloadEntity.setState(DownloadEntity.STATE_DOWNLOAD_ING);
sendInState2Target(DownloadSchedulers.START); sendInState2Target(DownloadSchedulers.START);
sendIntent(DownloadManager.ACTION_START, startLocation); //sendIntent(DownloadManager.ACTION_START, startLocation);
} }
@Override public void onProgress(long currentLocation) { @Override public void onProgress(long currentLocation) {
@ -259,7 +259,7 @@ public class Task {
downloadEntity.setCurrentProgress(currentLocation); downloadEntity.setCurrentProgress(currentLocation);
lastLen = currentLocation; lastLen = currentLocation;
sendInState2Target(DownloadSchedulers.RUNNING); sendInState2Target(DownloadSchedulers.RUNNING);
context.sendBroadcast(sendIntent); //context.sendBroadcast(sendIntent);
} }
} }
@ -268,14 +268,14 @@ public class Task {
downloadEntity.setState(DownloadEntity.STATE_STOP); downloadEntity.setState(DownloadEntity.STATE_STOP);
downloadEntity.setSpeed(0); downloadEntity.setSpeed(0);
sendInState2Target(DownloadSchedulers.STOP); sendInState2Target(DownloadSchedulers.STOP);
sendIntent(DownloadManager.ACTION_STOP, stopLocation); //sendIntent(DownloadManager.ACTION_STOP, stopLocation);
} }
@Override public void onCancel() { @Override public void onCancel() {
super.onCancel(); super.onCancel();
downloadEntity.setState(DownloadEntity.STATE_CANCEL); downloadEntity.setState(DownloadEntity.STATE_CANCEL);
sendInState2Target(DownloadSchedulers.CANCEL); sendInState2Target(DownloadSchedulers.CANCEL);
sendIntent(DownloadManager.ACTION_CANCEL, -1); //sendIntent(DownloadManager.ACTION_CANCEL, -1);
downloadEntity.deleteData(); downloadEntity.deleteData();
} }
@ -285,7 +285,7 @@ public class Task {
downloadEntity.setDownloadComplete(true); downloadEntity.setDownloadComplete(true);
downloadEntity.setSpeed(0); downloadEntity.setSpeed(0);
sendInState2Target(DownloadSchedulers.COMPLETE); sendInState2Target(DownloadSchedulers.COMPLETE);
sendIntent(DownloadManager.ACTION_COMPLETE, downloadEntity.getFileSize()); //sendIntent(DownloadManager.ACTION_COMPLETE, downloadEntity.getFileSize());
} }
@Override public void onFail() { @Override public void onFail() {
@ -293,7 +293,7 @@ public class Task {
downloadEntity.setState(DownloadEntity.STATE_FAIL); downloadEntity.setState(DownloadEntity.STATE_FAIL);
downloadEntity.setSpeed(0); downloadEntity.setSpeed(0);
sendInState2Target(DownloadSchedulers.FAIL); sendInState2Target(DownloadSchedulers.FAIL);
sendIntent(DownloadManager.ACTION_FAIL, -1); //sendIntent(DownloadManager.ACTION_FAIL, -1);
} }
private void sendIntent(String action, long location) { private void sendIntent(String action, long location) {

@ -14,7 +14,6 @@
* limitations under the License. * limitations under the License.
*/ */
package com.arialyy.downloadutil.orm; package com.arialyy.downloadutil.orm;
import android.app.Application; import android.app.Application;
@ -113,8 +112,7 @@ public class DbUtil {
int i = 0; int i = 0;
for (Field field : fields) { for (Field field : fields) {
field.setAccessible(true); field.setAccessible(true);
Ignore ignore = field.getAnnotation(Ignore.class); if (ignoreField(field)) {
if (ignore != null && ignore.value()) {
continue; continue;
} }
sb.append(i > 0 ? ", " : ""); sb.append(i > 0 ? ", " : "");
@ -195,8 +193,7 @@ public class DbUtil {
int i = 0; int i = 0;
for (Field field : fields) { for (Field field : fields) {
field.setAccessible(true); field.setAccessible(true);
Ignore ignore = field.getAnnotation(Ignore.class); if (ignoreField(field)) {
if (ignore != null && ignore.value()) {
continue; continue;
} }
sb.append(i > 0 ? ", " : ""); sb.append(i > 0 ? ", " : "");
@ -207,8 +204,7 @@ public class DbUtil {
i = 0; i = 0;
for (Field field : fields) { for (Field field : fields) {
field.setAccessible(true); field.setAccessible(true);
Ignore ignore = field.getAnnotation(Ignore.class); if (ignoreField(field)) {
if (ignore != null && ignore.value()) {
continue; continue;
} }
sb.append(i > 0 ? ", " : ""); sb.append(i > 0 ? ", " : "");
@ -270,8 +266,7 @@ public class DbUtil {
sb.append("create table ").append(CommonUtil.getClassName(clazz)).append("("); sb.append("create table ").append(CommonUtil.getClassName(clazz)).append("(");
for (Field field : fields) { for (Field field : fields) {
field.setAccessible(true); field.setAccessible(true);
Ignore ignore = field.getAnnotation(Ignore.class); if (ignoreField(field)) {
if (ignore != null && ignore.value()) {
continue; continue;
} }
sb.append(field.getName()); sb.append(field.getName());
@ -290,8 +285,10 @@ public class DbUtil {
sb.append(" boolean"); sb.append(" boolean");
} else if (type == java.util.Date.class || type == java.sql.Date.class) { } else if (type == java.util.Date.class || type == java.sql.Date.class) {
sb.append(" data"); sb.append(" data");
} else { } else if (type == byte.class || type == Byte.class) {
sb.append(" blob"); sb.append(" blob");
} else {
continue;
} }
sb.append(","); sb.append(",");
} }
@ -303,6 +300,15 @@ public class DbUtil {
close(); close();
} }
/**
* @return true 忽略该字段
*/
private boolean ignoreField(Field field) {
// field.isSynthetic(), 使用as热启动App时,AS会自动给你的clss添加change字段
Ignore ignore = field.getAnnotation(Ignore.class);
return (ignore != null && ignore.value()) || field.isSynthetic();
}
/** /**
* 打印数据库日志 * 打印数据库日志
* *
@ -403,8 +409,7 @@ public class DbUtil {
T entity = clazz.newInstance(); T entity = clazz.newInstance();
for (Field field : fields) { for (Field field : fields) {
field.setAccessible(true); field.setAccessible(true);
Ignore ignore = field.getAnnotation(Ignore.class); if (ignoreField(field)) {
if (ignore != null && ignore.value()) {
continue; continue;
} }
Class<?> type = field.getType(); Class<?> type = field.getType();
@ -425,6 +430,8 @@ public class DbUtil {
field.set(entity, new Date(cursor.getString(column))); field.set(entity, new Date(cursor.getString(column)));
} else if (type == byte[].class) { } else if (type == byte[].class) {
field.set(entity, cursor.getBlob(column)); field.set(entity, cursor.getBlob(column));
} else {
continue;
} }
} }
entity.rowID = cursor.getInt(cursor.getColumnIndex("rowid")); entity.rowID = cursor.getInt(cursor.getColumnIndex("rowid"));

Loading…
Cancel
Save