Skip to content

Commit

Permalink
update: 允许下自定义线程池looper
Browse files Browse the repository at this point in the history
  • Loading branch information
wyouflf committed Jun 8, 2020
1 parent 83e3a36 commit 86b5d2c
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 11 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ xUtils 包含了orm, http(s), image, view注解, 但依然很轻量级(251K),

#### 使用Gradle构建时添加以下依赖即可:
```javascript
implementation 'org.xutils:xutils:3.8.9'
implementation 'org.xutils:xutils:3.8.10'
```

#### 混淆配置参考示例项目sample的配置
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ buildscript {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.6.1'
classpath 'com.android.tools.build:gradle:4.0.0'
classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.8.4'
classpath 'com.github.dcendents:android-maven-gradle-plugin:2.1'

Expand Down
2 changes: 1 addition & 1 deletion sample/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@
<string name="remove">移除</string>
<string name="stop">停止</string>
<string name="start">开始下载</string>
<string name="test_download_url">http://dl.bintray.com/wyouflf/maven/org/xutils/xutils/3.8.9/xutils-3.8.9.aar</string>
<string name="test_download_url">http://dl.bintray.com/wyouflf/maven/org/xutils/xutils/3.8.10/xutils-3.8.10.aar</string>
</resources>
2 changes: 1 addition & 1 deletion xutils/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ apply plugin: 'com.jfrog.bintray'
def siteUrl = 'https://github.com/wyouflf/xUtils3'
def gitUrl = 'https://github.com/wyouflf/xUtils3.git'
group = "org.xutils"
version = "3.8.9"
version = "3.8.10"

android {
compileSdkVersion 28
Expand Down
6 changes: 6 additions & 0 deletions xutils/src/main/java/org/xutils/common/task/AbsTask.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.xutils.common.task;

import android.os.Looper;

import org.xutils.common.Callback;

import java.util.concurrent.Executor;
Expand Down Expand Up @@ -57,6 +59,10 @@ public Executor getExecutor() {
return null;
}

public Looper customLooper() {
return null;
}

protected final void update(int flag, Object... args) {
if (taskProxy != null) {
taskProxy.onUpdate(flag, args);
Expand Down
29 changes: 22 additions & 7 deletions xutils/src/main/java/org/xutils/common/task/TaskProxy.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

private final AbsTask<ResultType> task;
private final Executor executor;
private final Handler handler;
private volatile boolean callOnCanceled = false;
private volatile boolean callOnFinished = false;

Expand All @@ -30,6 +31,16 @@
this.task = task;
this.task.setTaskProxy(this);
this.setTaskProxy(null);

// set handler
Looper looper = task.customLooper();
if (looper != null) {
handler = new InternalHandler(looper);
} else {
handler = sHandler;
}

// set executor
Executor taskExecutor = task.getExecutor();
if (taskExecutor == null) {
taskExecutor = sDefaultExecutor;
Expand Down Expand Up @@ -85,42 +96,42 @@ public void run() {
@Override
protected void onWaiting() {
this.setState(State.WAITING);
sHandler.obtainMessage(MSG_WHAT_ON_WAITING, this).sendToTarget();
handler.obtainMessage(MSG_WHAT_ON_WAITING, this).sendToTarget();
}

@Override
protected void onStarted() {
this.setState(State.STARTED);
sHandler.obtainMessage(MSG_WHAT_ON_START, this).sendToTarget();
handler.obtainMessage(MSG_WHAT_ON_START, this).sendToTarget();
}

@Override
protected void onSuccess(ResultType result) {
this.setState(State.SUCCESS);
sHandler.obtainMessage(MSG_WHAT_ON_SUCCESS, this).sendToTarget();
handler.obtainMessage(MSG_WHAT_ON_SUCCESS, this).sendToTarget();
}

@Override
protected void onError(Throwable ex, boolean isCallbackError) {
this.setState(State.ERROR);
sHandler.obtainMessage(MSG_WHAT_ON_ERROR, new ArgsObj(this, ex)).sendToTarget();
handler.obtainMessage(MSG_WHAT_ON_ERROR, new ArgsObj(this, ex)).sendToTarget();
}

@Override
protected void onUpdate(int flag, Object... args) {
// obtainMessage(int what, int arg1, int arg2, Object obj), arg2 not be used.
sHandler.obtainMessage(MSG_WHAT_ON_UPDATE, flag, flag, new ArgsObj(this, args)).sendToTarget();
handler.obtainMessage(MSG_WHAT_ON_UPDATE, flag, flag, new ArgsObj(this, args)).sendToTarget();
}

@Override
protected void onCancelled(Callback.CancelledException cex) {
this.setState(State.CANCELLED);
sHandler.obtainMessage(MSG_WHAT_ON_CANCEL, new ArgsObj(this, cex)).sendToTarget();
handler.obtainMessage(MSG_WHAT_ON_CANCEL, new ArgsObj(this, cex)).sendToTarget();
}

@Override
protected void onFinished() {
sHandler.obtainMessage(MSG_WHAT_ON_FINISHED, this).sendToTarget();
handler.obtainMessage(MSG_WHAT_ON_FINISHED, this).sendToTarget();
}

@Override
Expand Down Expand Up @@ -165,6 +176,10 @@ private InternalHandler() {
super(Looper.getMainLooper());
}

private InternalHandler(Looper looper) {
super(looper);
}

@Override
@SuppressWarnings("unchecked")
public void handleMessage(Message msg) {
Expand Down

0 comments on commit 86b5d2c

Please sign in to comment.