启动逻辑修改

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

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

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

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

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

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

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

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

@ -14,7 +14,6 @@
* limitations under the License.
*/
package com.arialyy.downloadutil.orm;
import android.app.Application;
@ -113,8 +112,7 @@ public class DbUtil {
int i = 0;
for (Field field : fields) {
field.setAccessible(true);
Ignore ignore = field.getAnnotation(Ignore.class);
if (ignore != null && ignore.value()) {
if (ignoreField(field)) {
continue;
}
sb.append(i > 0 ? ", " : "");
@ -195,8 +193,7 @@ public class DbUtil {
int i = 0;
for (Field field : fields) {
field.setAccessible(true);
Ignore ignore = field.getAnnotation(Ignore.class);
if (ignore != null && ignore.value()) {
if (ignoreField(field)) {
continue;
}
sb.append(i > 0 ? ", " : "");
@ -207,8 +204,7 @@ public class DbUtil {
i = 0;
for (Field field : fields) {
field.setAccessible(true);
Ignore ignore = field.getAnnotation(Ignore.class);
if (ignore != null && ignore.value()) {
if (ignoreField(field)) {
continue;
}
sb.append(i > 0 ? ", " : "");
@ -270,8 +266,7 @@ public class DbUtil {
sb.append("create table ").append(CommonUtil.getClassName(clazz)).append("(");
for (Field field : fields) {
field.setAccessible(true);
Ignore ignore = field.getAnnotation(Ignore.class);
if (ignore != null && ignore.value()) {
if (ignoreField(field)) {
continue;
}
sb.append(field.getName());
@ -290,8 +285,10 @@ public class DbUtil {
sb.append(" boolean");
} else if (type == java.util.Date.class || type == java.sql.Date.class) {
sb.append(" data");
} else {
} else if (type == byte.class || type == Byte.class) {
sb.append(" blob");
} else {
continue;
}
sb.append(",");
}
@ -303,6 +300,15 @@ public class DbUtil {
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();
for (Field field : fields) {
field.setAccessible(true);
Ignore ignore = field.getAnnotation(Ignore.class);
if (ignore != null && ignore.value()) {
if (ignoreField(field)) {
continue;
}
Class<?> type = field.getType();
@ -425,6 +430,8 @@ public class DbUtil {
field.set(entity, new Date(cursor.getString(column)));
} else if (type == byte[].class) {
field.set(entity, cursor.getBlob(column));
} else {
continue;
}
}
entity.rowID = cursor.getInt(cursor.getColumnIndex("rowid"));

Loading…
Cancel
Save