├── .gitignore
├── LICENSE.md
├── README.md
├── app
├── .gitignore
├── build.gradle
├── proguard-rules.pro
└── src
│ └── main
│ ├── AndroidManifest.xml
│ ├── java
│ └── com
│ │ └── horizon
│ │ └── taskdemo
│ │ ├── activity
│ │ ├── ChainTestActivity.kt
│ │ ├── ConcurrentTestActivity.kt
│ │ ├── ConcurrentTestActivity2.kt
│ │ ├── CountingTestActivity.kt
│ │ ├── LoadingTestActivity.kt
│ │ ├── MainActivity.kt
│ │ ├── NotChainTestActivity.kt
│ │ └── SerialTestActivity.kt
│ │ ├── application
│ │ └── DemoApplication.kt
│ │ └── base
│ │ ├── BaseActivity.kt
│ │ ├── BaseFragment.kt
│ │ ├── HttpClient.kt
│ │ └── TaskSchedulers.kt
│ └── res
│ ├── drawable
│ └── ic_launcher_background.xml
│ ├── layout
│ ├── activity_chain_test.xml
│ ├── activity_concurrent_test.xml
│ ├── activity_concurrent_test2.xml
│ ├── activity_counting_test.xml
│ ├── activity_loading_test.xml
│ ├── activity_main.xml
│ └── activity_serial_test.xml
│ ├── mipmap-xhdpi
│ ├── ic_launcher.png
│ └── ic_launcher_round.png
│ ├── mipmap-xxhdpi
│ ├── ic_launcher.png
│ └── ic_launcher_round.png
│ └── values
│ ├── colors.xml
│ ├── strings.xml
│ └── styles.xml
├── build.gradle
├── gradle.properties
├── gradle
└── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── gradlew
├── gradlew.bat
├── settings.gradle
└── task
├── .gitignore
├── build.gradle
├── proguard-rules.pro
├── publish.gradle
└── src
└── main
├── AndroidManifest.xml
└── java
└── com
└── horizon
└── task
├── ChainTask.kt
├── TaskCenter.kt
├── UITask.kt
├── base
├── CircularQueue.kt
├── LogProxy.kt
├── Priority.kt
├── PriorityQueue.kt
└── TaskLogger.kt
├── executor
├── LaneExecutor.kt
├── PipeExecutor.kt
├── RunnableWrapper.kt
├── TaskExecutor.kt
└── Trigger.kt
└── lifecycle
├── Holder.kt
├── LifeEvent.kt
├── LifeListener.kt
└── LifecycleManager.kt
/.gitignore:
--------------------------------------------------------------------------------
1 | *.iml
2 | .gradle
3 | /local.properties
4 | .DS_Store
5 | /build
6 | /captures
7 | .externalNativeBuild
8 | .idea
9 |
--------------------------------------------------------------------------------
/LICENSE.md:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2021 Billy Wei
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 |
2 | ## Task
3 |
4 | AsyncTask Plus
5 |
6 | ## New Feature
7 | 1、More concurrency control
8 | 2、Support Priority
9 | 3、Support Task grouping
10 | 4、Support lifecycle
11 | 5、Support chain invocation
12 |
13 |
14 |
15 | ## Prepare
16 |
17 | 1. Initialization(optional)
18 |
19 | ```kotlin
20 | LogProxy.init(object : TaskLogger {
21 | override val isDebug: Boolean
22 | get() = BuildConfig.DEBUG
23 |
24 | override fun e(tag: String, e: Throwable) {
25 | Log.e(tag, e.message, e)
26 | }
27 | })
28 | ```
29 |
30 | 2. Notify Lifecycle Events
31 |
32 | ```kotlin
33 | abstract class BaseActivity : Activity() {
34 | override fun onDestroy() {
35 | super.onDestroy()
36 | LifecycleManager.notify(this, LifeEvent.DESTROY)
37 | }
38 |
39 | override fun onPause() {
40 | super.onPause()
41 | LifecycleManager.notify(this, LifeEvent.HIDE)
42 | }
43 |
44 | override fun onResume() {
45 | super.onResume()
46 | LifecycleManager.notify(this, LifeEvent.SHOW)
47 | }
48 | }
49 | ```
50 |
51 |
52 | ## How to use
53 | ### 1、Standard usage
54 |
55 | Just like AsyncTask:
56 |
57 | ```kotlin
58 | override fun onCreate(savedInstanceState: Bundle?) {
59 | // ...
60 | TestTask()
61 | .priority(Priority.IMMEDIATE)
62 | .host(this)
63 | .execute("hello")
64 | }
65 |
66 | private inner class TestTask: UITask(){
67 | override fun generateTag(): String {
68 | // Normally, you don't need to override this function
69 | return "custom tag"
70 | }
71 |
72 | override fun onPreExecute() {
73 | result_tv.text = "running"
74 | }
75 |
76 | override fun doInBackground(vararg params: String): String? {
77 | for (i in 0..100 step 2) {
78 | Thread.sleep(10)
79 | publishProgress(Integer(i))
80 | }
81 | return "result is:" + params[0].toUpperCase()
82 | }
83 |
84 | override fun onProgressUpdate(vararg values: Integer) {
85 | val progress = values[0]
86 | progress_bar.progress = progress.toInt()
87 | progress_tv.text = "$progress%"
88 | }
89 |
90 | override fun onPostExecute(result: String?) {
91 | result_tv.text = result
92 | }
93 |
94 | override fun onCancelled() {
95 | showTips("Task cancel ")
96 | }
97 | }
98 | ```
99 |
100 |
101 | ### 2、Executor
102 |
103 | ```
104 | TaskCenter.io.execute{
105 | // do something
106 | }
107 |
108 | TaskCenter.laneIO.execute("tag", {
109 | // do something
110 | })
111 |
112 | val serialExecutor = PipeExecutor(1)
113 | serialExecutor.execute{
114 | // do something
115 | }
116 |
117 | TaskCenter.serial.execute ("your tag", {
118 | // do something
119 | })
120 | ```
121 |
122 | ### 3、For RxJava
123 |
124 | ```kotlin
125 | object TaskSchedulers {
126 | val io: Scheduler by lazy { Schedulers.from(TaskCenter.io) }
127 | val computation: Scheduler by lazy { Schedulers.from(TaskCenter.computation) }
128 | val single by lazy { Schedulers.from(PipeExecutor(1)) }
129 | }
130 | ```
131 |
132 | ```kotlin
133 | Observable.range(1, 8)
134 | .subscribeOn(TaskSchedulers.computation)
135 | .subscribe { Log.d(tag, "number:$it") }
136 | ```
137 |
138 | ### 4、Chain invocation
139 | ```kotlin
140 | override fun onCreate(savedInstanceState: Bundle?) {
141 | val task = ChainTask()
142 | task.tag("ChainTest")
143 | .preExecute { result_tv.text = "running" }
144 | .background { params ->
145 | for (i in 0..100 step 2) {
146 | // do something
147 | task.publishProgress(i)
148 | }
149 | "result is:" + (params[0] * 100)
150 | }
151 | .progressUpdate { values ->
152 | val progress = values[0]
153 | progress_bar.progress = progress
154 | progress_tv.text = "$progress%"
155 | }
156 | .postExecute { result_tv.text = it }
157 | .cancel { showTips("ChainTask cancel") }
158 | .priority(Priority.IMMEDIATE)
159 | .host(this)
160 | .execute(3.14)
161 | }
162 | ```
163 |
164 | ## Link
165 | https://www.jianshu.com/p/8afb6cf64eec
166 |
167 |
168 |
169 | ## License
170 | See the [LICENSE](LICENSE.md) file for license rights and limitations.
171 |
172 |
173 |
--------------------------------------------------------------------------------
/app/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/app/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.application'
2 |
3 | apply plugin: 'kotlin-android'
4 | apply plugin: 'kotlin-android-extensions'
5 |
6 | android {
7 | compileSdkVersion 28
8 | defaultConfig {
9 | applicationId "com.horizon.taskdemo"
10 | minSdkVersion 14
11 | targetSdkVersion 28
12 | versionCode 1
13 | versionName "1.0"
14 | }
15 | buildTypes {
16 | debug{
17 |
18 | }
19 | release {
20 | minifyEnabled false
21 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
22 | }
23 | }
24 | }
25 |
26 |
27 |
28 | dependencies {
29 | implementation fileTree(dir: 'libs', include: ['*.jar'])
30 | implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
31 | implementation 'com.squareup.okhttp3:okhttp:3.12.0'
32 | implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.3.31"
33 | implementation 'io.reactivex.rxjava2:rxandroid:2.1.1'
34 | implementation "io.reactivex.rxjava2:rxjava:2.2.8"
35 | implementation project(':task')
36 | }
37 |
--------------------------------------------------------------------------------
/app/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # You can control the set of applied configuration files using the
3 | # proguardFiles setting in build.gradle.
4 | #
5 | # For more details, see
6 | # http://developer.android.com/guide/developing/tools/proguard.html
7 |
8 | # If your project uses WebView with JS, uncomment the following
9 | # and specify the fully qualified class name to the JavaScript interface
10 | # class:
11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
12 | # public *;
13 | #}
14 |
15 | # Uncomment this to preserve the line number information for
16 | # debugging stack traces.
17 | #-keepattributes SourceFile,LineNumberTable
18 |
19 | # If you keep the line number information, uncomment this to
20 | # hide the original source file name.
21 | #-renamesourcefileattribute SourceFile
22 |
--------------------------------------------------------------------------------
/app/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
6 |
7 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
--------------------------------------------------------------------------------
/app/src/main/java/com/horizon/taskdemo/activity/ChainTestActivity.kt:
--------------------------------------------------------------------------------
1 | package com.horizon.taskdemo.activity
2 |
3 | import android.annotation.SuppressLint
4 | import android.os.Bundle
5 | import com.horizon.task.ChainTask
6 | import com.horizon.task.base.Priority
7 | import com.horizon.taskdemo.R
8 | import com.horizon.taskdemo.base.BaseActivity
9 | import kotlinx.android.synthetic.main.activity_chain_test.*
10 |
11 | class ChainTestActivity : BaseActivity() {
12 |
13 | @SuppressLint("SetTextI18n")
14 | override fun onCreate(savedInstanceState: Bundle?) {
15 | super.onCreate(savedInstanceState)
16 | setContentView(R.layout.activity_chain_test)
17 |
18 | // 可在构造函数传入其他的TaskExecutor
19 | //val task = ChainTask(TaskCenter.laneCP)
20 |
21 | // 使用ChainTask,并且executor是LaneExecutor的话,最好设置tag, 否则讲退化为PipeExecutor。
22 | val task = ChainTask()
23 | task.tag("ChainTest")
24 | .preExecute { result_tv.text = "running" }
25 | .background { params ->
26 | for (i in 0..100 step 2) {
27 | Thread.sleep(10)
28 | task.publishProgress(i)
29 | }
30 | "result is:" + (params[0] * 100)
31 | }
32 | .progressUpdate { values ->
33 | val progress = values[0]
34 | progress_bar.progress = progress
35 | progress_tv.text = "$progress%"
36 | }
37 | .postExecute { result_tv.text = it }
38 | .cancel { showTips("ChainTask cancel ") }
39 | .priority(Priority.IMMEDIATE)
40 | .host(this)
41 | .execute(3.14)
42 | }
43 |
44 | }
--------------------------------------------------------------------------------
/app/src/main/java/com/horizon/taskdemo/activity/ConcurrentTestActivity.kt:
--------------------------------------------------------------------------------
1 | package com.horizon.taskdemo.activity
2 |
3 | import android.annotation.SuppressLint
4 | import android.os.Bundle
5 | import android.util.Log
6 | import com.horizon.task.TaskCenter
7 | import com.horizon.task.executor.PipeExecutor
8 | import com.horizon.taskdemo.R
9 | import com.horizon.taskdemo.base.BaseActivity
10 | import kotlinx.android.synthetic.main.activity_concurrent_test.*
11 | import java.util.concurrent.atomic.AtomicInteger
12 |
13 |
14 | class ConcurrentTestActivity : BaseActivity() {
15 |
16 | @SuppressLint("SetTextI18n")
17 | override fun onCreate(savedInstanceState: Bundle?) {
18 | super.onCreate(savedInstanceState)
19 | setContentView(R.layout.activity_concurrent_test)
20 |
21 | val a = AtomicInteger()
22 | val b = AtomicInteger()
23 | val c = AtomicInteger()
24 |
25 | TaskCenter.io.execute{
26 | // do something
27 | }
28 |
29 | TaskCenter.laneIO.execute("laneIO", {
30 | // do something
31 | })
32 |
33 | val serialExecutor = PipeExecutor(1)
34 | serialExecutor.execute{
35 | // do something
36 | }
37 |
38 | TaskCenter.serial.execute ("your tag", {
39 | // do something
40 | })
41 |
42 | // TaskCenter.io 没做任务去重,所以a=5
43 | for (i in 1..5) {
44 | TaskCenter.io.execute {
45 | Thread.sleep(100)
46 | Log.d(tag, "TaskCenter.io")
47 | a.incrementAndGet()
48 | }
49 | }
50 |
51 | // TaskCenter.serial 不会忽略任务(但会串行执行),所以b=5
52 | for (i in 1..5) {
53 | TaskCenter.serial.execute ("serial", {
54 | Thread.sleep(100)
55 | Log.d(tag, "TaskCenter.serial")
56 | b.incrementAndGet()
57 | })
58 | }
59 |
60 | // TaskCenter.laneIO 会只保留一个在等待的任务,后来者会被忽略,所以c=2
61 | for (i in 1..5) {
62 | TaskCenter.laneIO.execute("laneIO", {
63 | Thread.sleep(100)
64 | Log.d(tag, "TaskCenter.laneCP $i")
65 | c.incrementAndGet()
66 | })
67 | }
68 |
69 | // 观察log, 将会看到:
70 | // TaskCenter.io 几乎同时打印
71 | // TaskCenter.serial 每隔100ms打印一条
72 | // TaskCenter.laneIO 也是每隔100ms打印一条,但只会打印2条,
73 | // 因为在几乎同一时刻提交了5个任务,被过滤了3个
74 |
75 | state_tv.postDelayed({
76 | state_tv.text = "a:$a b:$b c:$c"
77 | }, 1000)
78 | }
79 |
80 |
81 | }
82 |
--------------------------------------------------------------------------------
/app/src/main/java/com/horizon/taskdemo/activity/ConcurrentTestActivity2.kt:
--------------------------------------------------------------------------------
1 | package com.horizon.taskdemo.activity
2 |
3 | import android.annotation.SuppressLint
4 | import android.os.Bundle
5 | import android.util.Log
6 | import android.widget.TextView
7 | import com.horizon.task.UITask
8 | import com.horizon.task.executor.LaneExecutor
9 | import com.horizon.task.executor.PipeExecutor
10 | import com.horizon.task.executor.TaskExecutor
11 | import com.horizon.taskdemo.R
12 | import com.horizon.taskdemo.base.BaseActivity
13 | import java.util.concurrent.CountDownLatch
14 | import java.util.concurrent.TimeUnit
15 |
16 | // 这个用例有点复杂, ConcurrentTestActivity会好理解一些
17 | class ConcurrentTestActivity2 : BaseActivity() {
18 | companion object {
19 | private const val CONCURRENT_SIZE = 4
20 | }
21 |
22 | private val mCustomExecutor = LaneExecutor(PipeExecutor(CONCURRENT_SIZE), true)
23 |
24 | private var mCount = 0
25 | internal lateinit var mProgressTv: TextView
26 | internal lateinit var mStateTv: TextView
27 | internal lateinit var mResultTv: TextView
28 |
29 | // 32个任务同时启动,8个可以马上执行,8个进入等待,16个被丢弃
30 | internal val mTagCount = 8
31 | internal val mExpectedCount = mTagCount * 2
32 | private val mTaskCount = mTagCount * 4
33 |
34 | internal val mSleepTime = 200
35 | private val mWaitTime = 1000L
36 | internal val mExpectedTime = mExpectedCount * mSleepTime / CONCURRENT_SIZE + mWaitTime
37 |
38 | override fun onCreate(savedInstanceState: Bundle?) {
39 | super.onCreate(savedInstanceState)
40 | setContentView(R.layout.activity_concurrent_test2)
41 |
42 | mStateTv = findViewById(R.id.state_tv)
43 | mProgressTv = findViewById(R.id.progess_tv)
44 | mResultTv = findViewById(R.id.result_tv)
45 |
46 | object : UITask() {
47 | private var actuallyTime: Long = 0
48 |
49 | override fun doInBackground(vararg params: Void): String? {
50 | val start = System.nanoTime()
51 | startTest()
52 | val end = System.nanoTime()
53 | actuallyTime = (end - start) / 1000000
54 | return null
55 | }
56 |
57 | @SuppressLint("SetTextI18n")
58 | override fun onPostExecute(result: String?) {
59 | mStateTv.text = "done"
60 |
61 | // Thread.sleep(long) 通常会多sleep()一点时间
62 | // 所以 mExpectedTim 一般预计的多一点
63 | // 但是mCount是准的,和mExpectedCount一样多
64 | mResultTv.text = ("use time:" + actuallyTime
65 | + "\nexpectedTime:" + mExpectedTime
66 | + "\nfinish count:" + mCount
67 | + "\nexpectedCount: " + mExpectedCount)
68 | }
69 | }
70 | .host(this)
71 | .execute()
72 |
73 | }
74 |
75 | private fun startTest() {
76 | val latch = CountDownLatch(mExpectedCount)
77 |
78 | for (i in 0 until mTaskCount) {
79 | // 如果 onPreExecute()不做UI操作
80 | // 在后台线程中启动UITask也是没有问题的
81 | object : UITask() {
82 | override val executor: TaskExecutor
83 | get() = mCustomExecutor
84 |
85 | override fun generateTag(): String {
86 | // 给任务打标签
87 | return "TASK" + i % mTagCount
88 | }
89 |
90 | override fun doInBackground(vararg params: Void): Void? {
91 | try {
92 | Thread.sleep(mSleepTime.toLong())
93 | } catch (e: InterruptedException) {
94 | Log.w(tag, e.javaClass.simpleName + " occurred")
95 | } finally {
96 | latch.countDown()
97 | }
98 | return null
99 | }
100 |
101 | @SuppressLint("SetTextI18n")
102 | override fun onPostExecute(result: Void?) {
103 | mProgressTv.text = (++mCount).toString() + " task finish"
104 | }
105 | }.host(this).execute()
106 | }
107 |
108 | try {
109 | latch.await((mExpectedTime + 3000), TimeUnit.MILLISECONDS)
110 |
111 | Thread.sleep(mWaitTime)
112 | } catch (e: InterruptedException) {
113 | Log.e(tag, e.message, e)
114 | }
115 |
116 | }
117 |
118 |
119 |
120 | }
121 |
--------------------------------------------------------------------------------
/app/src/main/java/com/horizon/taskdemo/activity/CountingTestActivity.kt:
--------------------------------------------------------------------------------
1 | package com.horizon.taskdemo.activity
2 |
3 | import android.os.Bundle
4 | import android.util.Log
5 | import android.widget.TextView
6 | import com.horizon.task.TaskCenter
7 | import com.horizon.task.UITask
8 | import com.horizon.task.base.Priority
9 | import com.horizon.task.executor.TaskExecutor
10 | import com.horizon.taskdemo.R
11 | import com.horizon.taskdemo.base.BaseActivity
12 |
13 |
14 | class CountingTestActivity : BaseActivity() {
15 | private lateinit var mCountingTv: TextView
16 |
17 | override fun onCreate(savedInstanceState: Bundle?) {
18 | super.onCreate(savedInstanceState)
19 | setContentView(R.layout.activity_counting_test)
20 |
21 | mCountingTv = findViewById(R.id.counting_tv)
22 |
23 | CountingTask()
24 | .host(this)
25 | .priority(Priority.IMMEDIATE)
26 | .execute(20 as Integer)
27 | }
28 |
29 | private inner class CountingTask : UITask(){
30 | // 事实上这里不需要 TaskCenter.laneCP, 只是为了展示使用方法
31 | override val executor: TaskExecutor
32 | get() = TaskCenter.laneCP
33 |
34 | override fun doInBackground(vararg params: Integer): String? {
35 | val n = params[0] as Int
36 | return try {
37 | for (count in n downTo 1) {
38 | publishProgress(Integer.toString(count))
39 | Thread.sleep(1000)
40 | }
41 | "done"
42 | } catch (e: InterruptedException) {
43 | Log.w(tag, e.javaClass.simpleName + " occurred")
44 | "cancel"
45 | }
46 | }
47 |
48 | override fun onProgressUpdate(vararg values: String) {
49 | mCountingTv.text = values[0]
50 | }
51 |
52 | override fun onCancelled(result: String?) {
53 | Log.i(tag, "task result: " + result!!)
54 | }
55 |
56 | override fun onPostExecute(result: String?) {
57 | mCountingTv.text = result
58 | }
59 | }
60 | }
61 |
--------------------------------------------------------------------------------
/app/src/main/java/com/horizon/taskdemo/activity/LoadingTestActivity.kt:
--------------------------------------------------------------------------------
1 | package com.horizon.taskdemo.activity
2 |
3 | import android.graphics.Bitmap
4 | import android.graphics.BitmapFactory
5 | import android.os.Bundle
6 | import android.util.Log
7 | import com.horizon.task.UITask
8 | import com.horizon.taskdemo.R
9 | import com.horizon.taskdemo.base.BaseActivity
10 | import com.horizon.taskdemo.base.HttpClient
11 | import kotlinx.android.synthetic.main.activity_loading_test.*
12 | import okhttp3.Request
13 |
14 |
15 | class LoadingTestActivity : BaseActivity() {
16 | override fun onCreate(savedInstanceState: Bundle?) {
17 | super.onCreate(savedInstanceState)
18 | setContentView(R.layout.activity_loading_test)
19 |
20 | val url = "https://pic1.zhimg.com/80/63536f2f01409f750162828a980a0380_hd.jpg"
21 | LoadingTask().host(this).execute(url)
22 | }
23 |
24 | private inner class LoadingTask : UITask() {
25 | override fun doInBackground(vararg params: String): Bitmap? {
26 | val url = params[0]
27 | val builder = Request.Builder().url(url)
28 | try {
29 | val response = HttpClient.execute(builder.build())
30 | if (response.isSuccessful) {
31 | return BitmapFactory.decodeStream(response.body()!!.byteStream())
32 | }
33 | } catch (e: Exception) {
34 | Log.e(tag, e.message, e)
35 | }
36 |
37 | return null
38 | }
39 |
40 | override fun onPostExecute(result: Bitmap?) {
41 | if (result != null) {
42 | test_iv.setImageBitmap(result)
43 | }
44 | }
45 | }
46 | }
47 |
--------------------------------------------------------------------------------
/app/src/main/java/com/horizon/taskdemo/activity/MainActivity.kt:
--------------------------------------------------------------------------------
1 | package com.horizon.taskdemo.activity
2 |
3 | import android.os.Bundle
4 | import android.util.Log
5 | import android.view.View
6 | import com.horizon.taskdemo.R
7 | import com.horizon.taskdemo.base.BaseActivity
8 | import com.horizon.taskdemo.base.TaskSchedulers
9 | import io.reactivex.Observable
10 | import io.reactivex.android.schedulers.AndroidSchedulers
11 | import io.reactivex.disposables.Disposable
12 | import kotlinx.android.synthetic.main.activity_main.*
13 |
14 | class MainActivity : BaseActivity(), View.OnClickListener {
15 | private var disposable: Disposable? = null
16 |
17 | override fun onCreate(savedInstanceState: Bundle?) {
18 | super.onCreate(savedInstanceState)
19 | setContentView(R.layout.activity_main)
20 |
21 | loading_btn.setOnClickListener(this)
22 | counting_btn.setOnClickListener(this)
23 | serial_btn.setOnClickListener(this)
24 | concurrent_btn.setOnClickListener(this)
25 | chain_btn.setOnClickListener(this)
26 |
27 | disposable = Observable.range(1, 8)
28 | .flatMap { i ->
29 | Observable.just(i)
30 | .subscribeOn(TaskSchedulers.computation)
31 | .map { Thread.sleep(1050); "param:$it" }
32 | }
33 | .observeOn(AndroidSchedulers.mainThread())
34 | .subscribe { result ->
35 | Log.d(tag, result)
36 | }
37 | }
38 |
39 | override fun onDestroy() {
40 | super.onDestroy()
41 | disposable?.dispose()
42 | }
43 |
44 | override fun onClick(v: View) {
45 | when (v.id) {
46 | R.id.loading_btn -> startActivity(LoadingTestActivity::class.java)
47 | R.id.counting_btn -> startActivity(CountingTestActivity::class.java)
48 | R.id.serial_btn -> startActivity(SerialTestActivity::class.java)
49 | R.id.concurrent_btn -> startActivity(ConcurrentTestActivity::class.java)
50 | R.id.chain_btn -> startActivity(ChainTestActivity::class.java)
51 | //R.id.chain_btn -> startActivity(NotChainTestActivity::class.java)
52 | }
53 | }
54 |
55 | }
56 |
57 |
--------------------------------------------------------------------------------
/app/src/main/java/com/horizon/taskdemo/activity/NotChainTestActivity.kt:
--------------------------------------------------------------------------------
1 | package com.horizon.taskdemo.activity
2 |
3 | import android.annotation.SuppressLint
4 | import android.os.Bundle
5 | import com.horizon.task.UITask
6 | import com.horizon.task.base.Priority
7 | import com.horizon.taskdemo.R
8 | import com.horizon.taskdemo.base.BaseActivity
9 | import kotlinx.android.synthetic.main.activity_chain_test.*
10 |
11 | // 此类用于与 ChainTestActivity对比
12 | class NotChainTestActivity : BaseActivity() {
13 | @SuppressLint("SetTextI18n")
14 | override fun onCreate(savedInstanceState: Bundle?) {
15 | super.onCreate(savedInstanceState)
16 | setContentView(R.layout.activity_chain_test)
17 |
18 | NotChainTask()
19 | .priority(Priority.IMMEDIATE)
20 | .host(this)
21 | .execute("hello")
22 | }
23 |
24 | private inner class NotChainTask : UITask() {
25 | override fun generateTag(): String {
26 | // 一般情况下不需要重写这个函数,这里只是为了演示
27 | return "custom tag"
28 | }
29 |
30 | override fun onPreExecute() {
31 | result_tv.text = "running"
32 | }
33 |
34 | override fun doInBackground(vararg params: String): String? {
35 | for (i in 0..100 step 2) {
36 | Thread.sleep(10)
37 | publishProgress(Integer(i))
38 | }
39 | return "result is:" + params[0].toUpperCase()
40 | }
41 |
42 | override fun onProgressUpdate(vararg values: Integer) {
43 | val progress = values[0]
44 | progress_bar.progress = progress.toInt()
45 | progress_tv.text = "$progress%"
46 | }
47 |
48 | override fun onPostExecute(result: String?) {
49 | result_tv.text = result
50 | }
51 |
52 | override fun onCancelled() {
53 | showTips("ChainTask cancel ")
54 | }
55 | }
56 |
57 |
58 | }
--------------------------------------------------------------------------------
/app/src/main/java/com/horizon/taskdemo/activity/SerialTestActivity.kt:
--------------------------------------------------------------------------------
1 | package com.horizon.taskdemo.activity
2 |
3 | import android.os.Bundle
4 | import android.util.Log
5 | import android.widget.TextView
6 | import com.horizon.task.executor.PipeExecutor
7 | import com.horizon.taskdemo.R
8 | import com.horizon.taskdemo.base.BaseActivity
9 |
10 |
11 | class SerialTestActivity : BaseActivity() {
12 | private var mCountingTv: TextView? = null
13 | private var destroy = false
14 | private var count = 0
15 | private var createTime: Long = 0
16 |
17 | private val mSerialExecutor = PipeExecutor(1)
18 |
19 | override fun onCreate(savedInstanceState: Bundle?) {
20 | super.onCreate(savedInstanceState)
21 | setContentView(R.layout.activity_serial_test)
22 |
23 | mCountingTv = findViewById(R.id.counting_tv)
24 | createTime = System.nanoTime()
25 | for (i in 0 until N) {
26 | // 两种方式实现串行:TaskCenter.serial更方便一些;PipeExecutor(1)更独立一些
27 | // TaskCenter.serial.execute("CountingTask", CountingTask())
28 | mSerialExecutor.execute(CountingTask())
29 | }
30 | }
31 |
32 | private inner class CountingTask : Runnable {
33 | override fun run() {
34 | count++
35 | Log.d(tag, "task count: $count")
36 |
37 | if (!destroy) {
38 | try {
39 | Thread.sleep(1000L)
40 | } catch (ignore: InterruptedException) {
41 | }
42 |
43 | val t = (System.nanoTime() - createTime) / 1000000
44 | val text = "$count task finished, \nuse time: $t"
45 | runOnUiThread { mCountingTv!!.text = text }
46 | }
47 | }
48 | }
49 |
50 | override fun onDestroy() {
51 | super.onDestroy()
52 | destroy = true
53 | }
54 |
55 | companion object {
56 | private const val N = 5
57 | }
58 | }
59 |
--------------------------------------------------------------------------------
/app/src/main/java/com/horizon/taskdemo/application/DemoApplication.kt:
--------------------------------------------------------------------------------
1 | package com.horizon.taskdemo.application
2 |
3 | import android.app.Application
4 | import android.content.Context
5 | import android.util.Log
6 |
7 | import com.horizon.task.base.LogProxy
8 | import com.horizon.task.base.TaskLogger
9 | import com.horizon.taskdemo.BuildConfig
10 | import com.horizon.taskdemo.base.HttpClient
11 |
12 |
13 | class DemoApplication : Application() {
14 |
15 | companion object {
16 | lateinit var context : Context
17 | }
18 |
19 | override fun onCreate() {
20 | super.onCreate()
21 |
22 | context = applicationContext
23 |
24 | HttpClient.init(this)
25 |
26 | LogProxy.init(object : TaskLogger {
27 | override val isDebug: Boolean
28 | get() = BuildConfig.DEBUG
29 |
30 | override fun e(tag: String, e: Throwable) {
31 | Log.e(tag, e.message, e)
32 | }
33 | })
34 | }
35 | }
36 |
--------------------------------------------------------------------------------
/app/src/main/java/com/horizon/taskdemo/base/BaseActivity.kt:
--------------------------------------------------------------------------------
1 | package com.horizon.taskdemo.base
2 |
3 | import android.app.Activity
4 | import android.content.Intent
5 | import android.widget.Toast
6 | import com.horizon.task.lifecycle.LifeEvent
7 |
8 | import com.horizon.task.lifecycle.LifecycleManager
9 | import com.horizon.taskdemo.application.DemoApplication
10 |
11 | abstract class BaseActivity : Activity() {
12 | protected val tag = this.javaClass.simpleName!!
13 |
14 | override fun onDestroy() {
15 | super.onDestroy()
16 | LifecycleManager.notify(this, LifeEvent.DESTROY)
17 | }
18 |
19 | override fun onPause() {
20 | super.onPause()
21 | LifecycleManager.notify(this, LifeEvent.HIDE)
22 | }
23 |
24 | override fun onResume() {
25 | super.onResume()
26 | LifecycleManager.notify(this, LifeEvent.SHOW)
27 | }
28 |
29 | fun startActivity(activityClazz: Class<*>) {
30 | val intent = Intent(this, activityClazz)
31 | startActivity(intent)
32 | }
33 |
34 | fun showTips(msg : String){
35 | Toast.makeText( DemoApplication.context,msg, Toast.LENGTH_SHORT ).show()
36 | }
37 | }
38 |
--------------------------------------------------------------------------------
/app/src/main/java/com/horizon/taskdemo/base/BaseFragment.kt:
--------------------------------------------------------------------------------
1 | package com.horizon.taskdemo.base
2 |
3 | import android.app.Fragment
4 | import com.horizon.task.lifecycle.LifeEvent
5 | import com.horizon.task.lifecycle.LifecycleManager
6 |
7 |
8 | abstract class BaseFragment : Fragment() {
9 | override fun onDestroy() {
10 | super.onDestroy()
11 | LifecycleManager.notify(this, LifeEvent.DESTROY)
12 | }
13 |
14 | override fun setUserVisibleHint(isVisibleToUser: Boolean) {
15 | super.setUserVisibleHint(isVisibleToUser)
16 | LifecycleManager.notify(this, if (isVisibleToUser) LifeEvent.SHOW else LifeEvent.HIDE)
17 | }
18 | }
--------------------------------------------------------------------------------
/app/src/main/java/com/horizon/taskdemo/base/HttpClient.kt:
--------------------------------------------------------------------------------
1 | package com.horizon.taskdemo.base
2 |
3 | import android.content.Context
4 | import okhttp3.Cache
5 | import okhttp3.OkHttpClient
6 | import okhttp3.Request
7 | import okhttp3.Response
8 | import java.io.File
9 | import java.io.IOException
10 |
11 |
12 | object HttpClient{
13 | private var client: OkHttpClient? = null
14 |
15 | fun init(context: Context) {
16 | if (client == null) {
17 | val cacheDirPath = context.cacheDir.path + "/http/"
18 | client = OkHttpClient.Builder()
19 | .cache(Cache(File(cacheDirPath), (64 shl 20).toLong()))
20 | .build()
21 | }
22 | }
23 |
24 | @Throws(IOException::class)
25 | fun execute(request: Request): Response {
26 | return client!!.newCall(request).execute()
27 | }
28 |
29 | }
30 |
--------------------------------------------------------------------------------
/app/src/main/java/com/horizon/taskdemo/base/TaskSchedulers.kt:
--------------------------------------------------------------------------------
1 | package com.horizon.taskdemo.base
2 |
3 | import com.horizon.task.TaskCenter
4 | import com.horizon.task.executor.PipeExecutor
5 | import io.reactivex.Scheduler
6 | import io.reactivex.schedulers.Schedulers
7 |
8 | object TaskSchedulers {
9 | val io: Scheduler by lazy { Schedulers.from(TaskCenter.io) }
10 | val computation: Scheduler by lazy { Schedulers.from(TaskCenter.computation) }
11 | val single by lazy { Schedulers.from(PipeExecutor(1)) }
12 | }
13 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable/ic_launcher_background.xml:
--------------------------------------------------------------------------------
1 |
2 |
7 |
10 |
15 |
20 |
25 |
30 |
35 |
40 |
45 |
50 |
55 |
60 |
65 |
70 |
75 |
80 |
85 |
90 |
95 |
100 |
105 |
110 |
115 |
120 |
125 |
130 |
135 |
140 |
145 |
150 |
155 |
160 |
165 |
170 |
171 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_chain_test.xml:
--------------------------------------------------------------------------------
1 |
2 |
7 |
8 |
23 |
24 |
35 |
36 |
44 |
45 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_concurrent_test.xml:
--------------------------------------------------------------------------------
1 |
2 |
8 |
9 |
10 |
19 |
20 |
21 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_concurrent_test2.xml:
--------------------------------------------------------------------------------
1 |
2 |
9 |
10 |
18 |
19 |
20 |
28 |
29 |
30 |
37 |
38 |
39 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_counting_test.xml:
--------------------------------------------------------------------------------
1 |
2 |
6 |
7 |
15 |
16 |
17 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_loading_test.xml:
--------------------------------------------------------------------------------
1 |
2 |
6 |
7 |
13 |
14 |
15 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_main.xml:
--------------------------------------------------------------------------------
1 |
2 |
8 |
9 |
17 |
18 |
19 |
27 |
28 |
36 |
37 |
45 |
46 |
54 |
55 |
56 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_serial_test.xml:
--------------------------------------------------------------------------------
1 |
2 |
6 |
7 |
16 |
17 |
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/BillyWei01/Task/59e2770b01b18b2a834332da18885b760025d91e/app/src/main/res/mipmap-xhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/BillyWei01/Task/59e2770b01b18b2a834332da18885b760025d91e/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/BillyWei01/Task/59e2770b01b18b2a834332da18885b760025d91e/app/src/main/res/mipmap-xxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/BillyWei01/Task/59e2770b01b18b2a834332da18885b760025d91e/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/values/colors.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | #3F51B5
4 | #303F9F
5 | #FF4081
6 |
7 |
--------------------------------------------------------------------------------
/app/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | TaskDemo
3 | NEXT
4 | counting
5 | loading test
6 | counting test
7 | concurrent test
8 | stand by
9 | 0 task finish
10 | use time: 0ms
11 | running
12 | serial test
13 | ready
14 | chain test
15 |
16 |
--------------------------------------------------------------------------------
/app/src/main/res/values/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
8 |
9 |
10 |
15 |
16 |
19 |
20 |
21 |
--------------------------------------------------------------------------------
/build.gradle:
--------------------------------------------------------------------------------
1 | // Top-level build file where you can add configuration options common to all sub-projects/modules.
2 |
3 | buildscript {
4 |
5 | repositories {
6 | google()
7 | jcenter()
8 | }
9 | dependencies {
10 | classpath 'com.android.tools.build:gradle:3.4.2'
11 |
12 | classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.3.31"
13 | classpath "org.jetbrains.dokka:dokka-android-gradle-plugin:0.9.17"
14 |
15 | //classpath 'com.getkeepsafe.dexcount:dexcount-gradle-plugin:0.6.2'
16 | //classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.7.3'
17 | //classpath "com.github.dcendents:android-maven-gradle-plugin:2.0"
18 |
19 | // NOTE: Do not place your application dependencies here; they belong
20 | // in the individual module build.gradle files
21 | }
22 | }
23 |
24 | allprojects {
25 | repositories {
26 | google()
27 | jcenter()
28 | }
29 | }
30 |
31 |
32 |
33 | task clean(type: Delete) {
34 | delete rootProject.buildDir
35 | }
36 |
--------------------------------------------------------------------------------
/gradle.properties:
--------------------------------------------------------------------------------
1 | # Project-wide Gradle settings.
2 |
3 | # IDE (e.g. Android Studio) users:
4 | # Gradle settings configured through the IDE *will override*
5 | # any settings specified in this file.
6 |
7 | # For more details on how to configure your build environment visit
8 | # http://www.gradle.org/docs/current/userguide/build_environment.html
9 |
10 | # Specifies the JVM arguments used for the daemon process.
11 | # The setting is particularly useful for tweaking memory settings.
12 | android.enableJetifier=true
13 | android.useAndroidX=true
14 | org.gradle.jvmargs=-Xmx1536m
15 | #android.overridePathCheck=true
16 |
17 | # When configured, Gradle will run in incubating parallel mode.
18 | # This option should only be used with decoupled projects. More details, visit
19 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
20 | org.gradle.parallel=true
21 |
22 | #systemProp.https.proxyHost=localhost
23 | #systemProp.https.proxyPort=49901
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/BillyWei01/Task/59e2770b01b18b2a834332da18885b760025d91e/gradle/wrapper/gradle-wrapper.jar
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.properties:
--------------------------------------------------------------------------------
1 | #Sat Sep 28 08:08:56 CST 2019
2 | distributionBase=GRADLE_USER_HOME
3 | distributionPath=wrapper/dists
4 | zipStoreBase=GRADLE_USER_HOME
5 | zipStorePath=wrapper/dists
6 | distributionUrl=https\://services.gradle.org/distributions/gradle-5.1.1-all.zip
7 |
--------------------------------------------------------------------------------
/gradlew:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env bash
2 |
3 | ##############################################################################
4 | ##
5 | ## Gradle start up script for UN*X
6 | ##
7 | ##############################################################################
8 |
9 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
10 | DEFAULT_JVM_OPTS=""
11 |
12 | APP_NAME="Gradle"
13 | APP_BASE_NAME=`basename "$0"`
14 |
15 | # Use the maximum available, or set MAX_FD != -1 to use that value.
16 | MAX_FD="maximum"
17 |
18 | warn ( ) {
19 | echo "$*"
20 | }
21 |
22 | die ( ) {
23 | echo
24 | echo "$*"
25 | echo
26 | exit 1
27 | }
28 |
29 | # OS specific support (must be 'true' or 'false').
30 | cygwin=false
31 | msys=false
32 | darwin=false
33 | case "`uname`" in
34 | CYGWIN* )
35 | cygwin=true
36 | ;;
37 | Darwin* )
38 | darwin=true
39 | ;;
40 | MINGW* )
41 | msys=true
42 | ;;
43 | esac
44 |
45 | # Attempt to set APP_HOME
46 | # Resolve links: $0 may be a link
47 | PRG="$0"
48 | # Need this for relative symlinks.
49 | while [ -h "$PRG" ] ; do
50 | ls=`ls -ld "$PRG"`
51 | link=`expr "$ls" : '.*-> \(.*\)$'`
52 | if expr "$link" : '/.*' > /dev/null; then
53 | PRG="$link"
54 | else
55 | PRG=`dirname "$PRG"`"/$link"
56 | fi
57 | done
58 | SAVED="`pwd`"
59 | cd "`dirname \"$PRG\"`/" >/dev/null
60 | APP_HOME="`pwd -P`"
61 | cd "$SAVED" >/dev/null
62 |
63 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
64 |
65 | # Determine the Java command to use to start the JVM.
66 | if [ -n "$JAVA_HOME" ] ; then
67 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
68 | # IBM's JDK on AIX uses strange locations for the executables
69 | JAVACMD="$JAVA_HOME/jre/sh/java"
70 | else
71 | JAVACMD="$JAVA_HOME/bin/java"
72 | fi
73 | if [ ! -x "$JAVACMD" ] ; then
74 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
75 |
76 | Please set the JAVA_HOME variable in your environment to match the
77 | location of your Java installation."
78 | fi
79 | else
80 | JAVACMD="java"
81 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
82 |
83 | Please set the JAVA_HOME variable in your environment to match the
84 | location of your Java installation."
85 | fi
86 |
87 | # Increase the maximum file descriptors if we can.
88 | if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then
89 | MAX_FD_LIMIT=`ulimit -H -n`
90 | if [ $? -eq 0 ] ; then
91 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
92 | MAX_FD="$MAX_FD_LIMIT"
93 | fi
94 | ulimit -n $MAX_FD
95 | if [ $? -ne 0 ] ; then
96 | warn "Could not set maximum file descriptor limit: $MAX_FD"
97 | fi
98 | else
99 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
100 | fi
101 | fi
102 |
103 | # For Darwin, add options to specify how the application appears in the dock
104 | if $darwin; then
105 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
106 | fi
107 |
108 | # For Cygwin, switch paths to Windows format before running java
109 | if $cygwin ; then
110 | APP_HOME=`cygpath --path --mixed "$APP_HOME"`
111 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
112 | JAVACMD=`cygpath --unix "$JAVACMD"`
113 |
114 | # We build the pattern for arguments to be converted via cygpath
115 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
116 | SEP=""
117 | for dir in $ROOTDIRSRAW ; do
118 | ROOTDIRS="$ROOTDIRS$SEP$dir"
119 | SEP="|"
120 | done
121 | OURCYGPATTERN="(^($ROOTDIRS))"
122 | # Add a user-defined pattern to the cygpath arguments
123 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then
124 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
125 | fi
126 | # Now convert the arguments - kludge to limit ourselves to /bin/sh
127 | i=0
128 | for arg in "$@" ; do
129 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
130 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
131 |
132 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
133 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
134 | else
135 | eval `echo args$i`="\"$arg\""
136 | fi
137 | i=$((i+1))
138 | done
139 | case $i in
140 | (0) set -- ;;
141 | (1) set -- "$args0" ;;
142 | (2) set -- "$args0" "$args1" ;;
143 | (3) set -- "$args0" "$args1" "$args2" ;;
144 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;;
145 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
146 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
147 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
148 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
149 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
150 | esac
151 | fi
152 |
153 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules
154 | function splitJvmOpts() {
155 | JVM_OPTS=("$@")
156 | }
157 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS
158 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME"
159 |
160 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@"
161 |
--------------------------------------------------------------------------------
/gradlew.bat:
--------------------------------------------------------------------------------
1 | @if "%DEBUG%" == "" @echo off
2 | @rem ##########################################################################
3 | @rem
4 | @rem Gradle startup script for Windows
5 | @rem
6 | @rem ##########################################################################
7 |
8 | @rem Set local scope for the variables with windows NT shell
9 | if "%OS%"=="Windows_NT" setlocal
10 |
11 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
12 | set DEFAULT_JVM_OPTS=
13 |
14 | set DIRNAME=%~dp0
15 | if "%DIRNAME%" == "" set DIRNAME=.
16 | set APP_BASE_NAME=%~n0
17 | set APP_HOME=%DIRNAME%
18 |
19 | @rem Find java.exe
20 | if defined JAVA_HOME goto findJavaFromJavaHome
21 |
22 | set JAVA_EXE=java.exe
23 | %JAVA_EXE% -version >NUL 2>&1
24 | if "%ERRORLEVEL%" == "0" goto init
25 |
26 | echo.
27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
28 | echo.
29 | echo Please set the JAVA_HOME variable in your environment to match the
30 | echo location of your Java installation.
31 |
32 | goto fail
33 |
34 | :findJavaFromJavaHome
35 | set JAVA_HOME=%JAVA_HOME:"=%
36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe
37 |
38 | if exist "%JAVA_EXE%" goto init
39 |
40 | echo.
41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
42 | echo.
43 | echo Please set the JAVA_HOME variable in your environment to match the
44 | echo location of your Java installation.
45 |
46 | goto fail
47 |
48 | :init
49 | @rem Get command-line arguments, handling Windowz variants
50 |
51 | if not "%OS%" == "Windows_NT" goto win9xME_args
52 | if "%@eval[2+2]" == "4" goto 4NT_args
53 |
54 | :win9xME_args
55 | @rem Slurp the command line arguments.
56 | set CMD_LINE_ARGS=
57 | set _SKIP=2
58 |
59 | :win9xME_args_slurp
60 | if "x%~1" == "x" goto execute
61 |
62 | set CMD_LINE_ARGS=%*
63 | goto execute
64 |
65 | :4NT_args
66 | @rem Get arguments from the 4NT Shell from JP Software
67 | set CMD_LINE_ARGS=%$
68 |
69 | :execute
70 | @rem Setup the command line
71 |
72 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
73 |
74 | @rem Execute Gradle
75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%
76 |
77 | :end
78 | @rem End local scope for the variables with windows NT shell
79 | if "%ERRORLEVEL%"=="0" goto mainEnd
80 |
81 | :fail
82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
83 | rem the _cmd.exe /c_ return code!
84 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
85 | exit /b 1
86 |
87 | :mainEnd
88 | if "%OS%"=="Windows_NT" endlocal
89 |
90 | :omega
91 |
--------------------------------------------------------------------------------
/settings.gradle:
--------------------------------------------------------------------------------
1 | include ':app', ':task'
2 |
--------------------------------------------------------------------------------
/task/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/task/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.library'
2 | apply plugin: 'kotlin-android'
3 |
4 | android {
5 | compileSdkVersion 28
6 |
7 | defaultConfig {
8 | minSdkVersion 14
9 | targetSdkVersion 28
10 | versionCode 1
11 | versionName "1.0"
12 | }
13 |
14 | buildTypes {
15 | debug{
16 |
17 | }
18 | release {
19 | minifyEnabled false
20 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
21 | }
22 | }
23 |
24 | }
25 |
26 | dependencies {
27 | implementation fileTree(dir: 'libs', include: ['*.jar'])
28 | implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.3.31"
29 | }
30 |
31 | //apply from: 'publish.gradle'
--------------------------------------------------------------------------------
/task/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # You can control the set of applied configuration files using the
3 | # proguardFiles setting in build.gradle.
4 | #
5 | # For more details, see
6 | # http://developer.android.com/guide/developing/tools/proguard.html
7 |
8 | # If your project uses WebView with JS, uncomment the following
9 | # and specify the fully qualified class name to the JavaScript interface
10 | # class:
11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
12 | # public *;
13 | #}
14 |
15 | # Uncomment this to preserve the line number information for
16 | # debugging stack traces.
17 | #-keepattributes SourceFile,LineNumberTable
18 |
19 | # If you keep the line number information, uncomment this to
20 | # hide the original source file name.
21 | #-renamesourcefileattribute SourceFile
22 |
--------------------------------------------------------------------------------
/task/publish.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.jfrog.bintray'
2 | apply plugin: 'com.github.dcendents.android-maven'
3 | apply plugin: 'org.jetbrains.dokka-android'
4 |
5 | def GROUP = 'com.horizon.task'
6 | def ARTIFACT = 'task'
7 | def VERSION = '1.0.6'
8 |
9 | group = GROUP
10 | version = VERSION
11 |
12 | dokka{
13 | outputFormat = 'javadoc'
14 | moduleName = project.name
15 | outputDirectory = "$buildDir/docs"
16 | }
17 |
18 | task javadocJar(type: Jar, dependsOn: dokka) {
19 | //classifier = 'javadoc'
20 | getArchiveClassifier().set('javadoc')
21 | from dokka.outputDirectory
22 | }
23 |
24 | task sourcesJar(type: Jar) {
25 | //classifier = 'sources'
26 | getArchiveClassifier().set('sources')
27 | from android.sourceSets.main.java.srcDirs
28 | }
29 |
30 | def srcClassDir = [project.buildDir.absolutePath + "/intermediates/classes/release",
31 | project.buildDir.absolutePath + "/tmp/kotlin-classes/release"]
32 |
33 | task makeJar(dependsOn: ['compileReleaseSources'], type: Jar) {
34 | delete 'build/libs/' +ARTIFACT + '.jar'
35 | baseName ARTIFACT
36 | extension = "jar"
37 | from srcClassDir
38 | exclude('**/R.class')
39 | exclude('**/BuildConfig.class')
40 | exclude { it.name.startsWith('R$') }
41 | }
42 |
43 | artifacts {
44 | archives javadocJar
45 | archives sourcesJar
46 | archives makeJar
47 | }
48 |
49 | def siteUrl = 'https://github.com/No89757/Task'
50 | def gitUrl = 'https://github.com/No89757/Task.git'
51 |
52 | install {
53 | repositories.mavenInstaller {
54 | pom {
55 | project {
56 | packaging 'aar'
57 | // Add your description here
58 | name 'Task is a thread scheduling framework.'
59 | url siteUrl
60 | licenses {
61 | license {
62 | name 'The Apache Software License, Version 2.0'
63 | url 'http://www.apache.org/licenses/LICENSE-2.0.txt'
64 | }
65 | }
66 | developers {
67 | developer {
68 | id 'no89757'
69 | name 'John Smith'
70 | email 'across_horizon@qq.com'
71 | }
72 | }
73 | scm {
74 | connection gitUrl
75 | developerConnection gitUrl
76 | url siteUrl
77 | }
78 | }
79 | }
80 | }
81 | }
82 |
83 | Properties properties = new Properties()
84 | properties.load(project.rootProject.file('local.properties').newDataInputStream())
85 | bintray {
86 | user = properties.getProperty("bintray.user")
87 | key = properties.getProperty("bintray.apikey")
88 | configurations = ['archives']
89 | pkg {
90 | userOrg = "horizon757"
91 | repo = "maven"
92 | name = "Task"
93 | websiteUrl = siteUrl
94 | vcsUrl = gitUrl
95 | licenses = ["Apache-2.0"]
96 | publish = true
97 | }
98 | }
99 |
--------------------------------------------------------------------------------
/task/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
--------------------------------------------------------------------------------
/task/src/main/java/com/horizon/task/ChainTask.kt:
--------------------------------------------------------------------------------
1 | package com.horizon.task
2 |
3 | import com.horizon.task.executor.TaskExecutor
4 |
5 | /**
6 | * 拓展[UITask],支持链式调用。
7 | *
8 | * 可以在构造函数指定要使用的 Executor。
9 | */
10 | class ChainTask(override val executor: TaskExecutor = TaskCenter.laneIO)
11 | : UITask() {
12 |
13 | private var tag: String? = null
14 |
15 | private var mPreExecute: (() -> Unit)? = null
16 | private var mBackground: ((params: List) -> Result?)? = null
17 | private var mPostExecute: ((result: Result?) -> Unit)? = null
18 | private var mProgressUpdate: ((values: List