├── .gitignore ├── LICENSE ├── Maat项目.png ├── README.md ├── app ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── osp │ │ └── leobert │ │ └── android │ │ └── maatsample │ │ └── ExampleInstrumentedTest.kt │ ├── main │ ├── AndroidManifest.xml │ ├── java │ │ └── osp │ │ │ └── leobert │ │ │ └── android │ │ │ └── maatsample │ │ │ ├── App.kt │ │ │ ├── MainActivity.kt │ │ │ └── MockAsync.kt │ └── res │ │ ├── drawable-v24 │ │ └── ic_launcher_foreground.xml │ │ ├── drawable │ │ └── ic_launcher_background.xml │ │ ├── layout │ │ └── activity_main.xml │ │ ├── mipmap-anydpi-v26 │ │ ├── ic_launcher.xml │ │ └── ic_launcher_round.xml │ │ ├── mipmap-hdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-mdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-xhdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-xxhdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-xxxhdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ └── values │ │ ├── colors.xml │ │ ├── strings.xml │ │ └── styles.xml │ └── test │ └── java │ └── osp │ └── leobert │ └── android │ └── maatsample │ └── ExampleUnitTest.kt ├── bintrayv1.gradle ├── build.gradle ├── gradle.properties ├── installv1.gradle ├── maat ├── .gitignore ├── build.gradle ├── consumer-rules.pro ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── osp │ │ └── leobert │ │ └── android │ │ └── maat │ │ └── ExampleInstrumentedTest.kt │ ├── main │ ├── AndroidManifest.xml │ └── java │ │ └── osp │ │ └── leobert │ │ └── android │ │ └── maat │ │ ├── JOB.kt │ │ ├── JobChunk.kt │ │ ├── Maat.kt │ │ ├── MaatException.kt │ │ ├── MaatUtil.kt │ │ ├── dag │ │ ├── DAG.kt │ │ ├── Edge.kt │ │ └── Type.kt │ │ └── dispatchers.kt │ └── test │ └── java │ └── osp │ └── leobert │ └── android │ └── maat │ └── ExampleUnitTest.kt └── settings.gradle /.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | .gradle 3 | /local.properties 4 | /.idea/caches 5 | /.idea/libraries 6 | /.idea/modules.xml 7 | /.idea/workspace.xml 8 | /.idea/navEditor.xml 9 | /.idea/assetWizardSettings.xml 10 | .DS_Store 11 | /build 12 | /captures 13 | .externalNativeBuild 14 | .cxx 15 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [2020] [Leobert] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /Maat项目.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/leobert-lan/Maat/c4c1a37a9d68e2c65624de4ee796803359b5d074/Maat项目.png -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Maat 2 | Maat是秩序之神,在之前参与开发的组件化项目JIMU中,一直被要求添加按序加载组件Component的功能,于是开始开发这个Android组件加载库,他可以单独使用,并不一定依赖于JIMU项目 3 | 4 | 相关博客[组件化:代码隔离也难不倒组件的按序初始化 ](https://juejin.im/post/6884492604370026503/) 5 | JIMU项目请看[这里](https://github.com/mqzhangw/JIMU) 6 | 7 | ## 最新版本: 8 | 9 | version|Maat 10 | ---|--- 11 | 最新版本|[![Download](https://api.bintray.com/packages/leobert-lan-oss/maven/maat/images/download.svg)](https://api.bintray.com/packages/leobert-lan-oss/maven/maat/_latestVersion) 12 | 13 | ## 解决的问题: 14 | 组件化场景(泛化到存在代码隔离的模块化场景)下,组件按序初始化的问题,有效解决“前置条件”,“线程灵活”。 15 | 16 | > 组件化的基础是模块化,在做到模块化的同时,模块与模块在编写、编译期间也就达成了完全代码隔离,组件间的交互依靠 底层接口+服务发现(或者服务注册) 或者更加抽象为 “基于协议、隐藏实现”。 17 | > 18 | > 这种场景下,想要用比较原始的、直面问题的方式解决组件按序初始化问题,例如使用反射+无分支遗漏的逻辑涵盖所有组件组合情况,会导致耦合激增。 19 | 20 | ## 功能特点 21 | 22 | * 线程灵活 23 | * 依赖缺失校验、依赖成环校验 24 | 25 | ## 集成: 26 | 27 | ``` 28 | implementation 'osp.leobert.android:maat:1.0.0' 29 | ``` 30 | 31 | 考虑到jcenter审核需要时间,未被收录时可添加仓库: 32 | 33 | ``` 34 | allprojects { 35 | repositories { 36 | maven { 37 | url 'https://dl.bintray.com/leobert-lan-oss/maven/' 38 | } 39 | } 40 | } 41 | ``` 42 | 43 | 初始化: 44 | 45 | ``` 46 | val maat = Maat.init(application = this, printChunkMax = 6, 47 | logger = object : Maat.Logger() { 48 | //日志功能开关 49 | override val enable: Boolean = true 50 | 51 | 52 | override fun log(msg: String, throws: Throwable?) { 53 | Log.d("maat", msg, throws) 54 | } 55 | 56 | }, callback = Maat.Callback(onSuccess = {}, onFailure = { maat, job, throwable -> 57 | 58 | }) 59 | ) 60 | ``` 61 | 62 | printChunkMax 是打印信息输出时比较理想的单元长度,取所有任务名长度的最大值即可,输出会比较美观。例如: 63 | 64 | ``` 65 | a start d b c 66 | X\Y ------------------------------ 67 | a |0 0 1 1 1 68 | start |1 0 0 0 0 69 | d |0 0 0 0 0 70 | b |0 0 1 0 0 71 | c |0 0 1 0 0 72 | ``` 73 | 74 | 添加任务: 75 | 76 | ``` 77 | maat.append(object : JOB() { 78 | override val uniqueKey: String = "a" 79 | override val dependsOn: List = emptyList() 80 | override val dispatcher: CoroutineDispatcher = Dispatchers.IO 81 | 82 | override fun init(maat: Maat) { 83 | Log.e( 84 | "maat", 85 | "run:" + uniqueKey + " isMain:" + (Looper.getMainLooper() == Looper.myLooper()) 86 | ) 87 | //test exception 88 | // throw NullPointerException("just a test") 89 | } 90 | 91 | override fun toString(): String { 92 | return uniqueKey 93 | } 94 | 95 | }).append(object : JOB() { 96 | override val uniqueKey: String = "b" 97 | override val dependsOn: List = arrayListOf("a") 98 | override val dispatcher: CoroutineDispatcher = Dispatchers.Main /* + Job()*/ 99 | 100 | override fun init(maat: Maat) { 101 | Log.e( 102 | "maat", 103 | "run:" + uniqueKey + " isMain:" + (Looper.getMainLooper() == Looper.myLooper()) 104 | ) 105 | } 106 | 107 | override fun toString(): String { 108 | return uniqueKey 109 | } 110 | 111 | }).append(object : JOB() { 112 | override val uniqueKey: String = "c" 113 | override val dependsOn: List = arrayListOf("a") 114 | override val dispatcher: CoroutineDispatcher = Dispatchers.IO /* + Job()*/ 115 | 116 | override fun init(maat: Maat) { 117 | Log.e( 118 | "maat", 119 | "run:" + uniqueKey + " isMain:" + (Looper.getMainLooper() == Looper.myLooper()) 120 | ) 121 | } 122 | 123 | override fun toString(): String { 124 | return uniqueKey 125 | } 126 | 127 | }).append(object : JOB() { 128 | override val uniqueKey: String = "d" 129 | override val dependsOn: List = arrayListOf("a", "b", "c") 130 | override val dispatcher: CoroutineDispatcher = Dispatchers.Main 131 | 132 | override fun init(maat: Maat) { 133 | Log.e( 134 | "maat", 135 | "run:" + uniqueKey + " isMain:" + (Looper.getMainLooper() == Looper.myLooper()) 136 | ) 137 | } 138 | 139 | override fun toString(): String { 140 | return uniqueKey 141 | } 142 | 143 | }) 144 | ``` 145 | 146 | 开始初始化: 147 | 148 | ``` 149 | maat.start() 150 | ``` 151 | 152 | Maat中实现了单例,初始化后可以用 153 | 154 | ``` 155 | Maat.getDefault() 156 | ``` 157 | 获取初始化的单例。**添加任务必须在初始化之后、start之前** 158 | 159 | 确信任务完成后不再需要Maat了可以释放实例: 160 | 161 | ``` 162 | Maat.release() 163 | ``` 164 | 165 | 更多细节: 166 | [分析与设计](https://juejin.im/post/6884492604370026503/#heading-8) 以及阅读源码 167 | 168 | 欢迎提供您宝贵的意见。 169 | -------------------------------------------------------------------------------- /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | apply plugin: 'kotlin-android' 3 | apply plugin: 'kotlin-android-extensions' 4 | 5 | android { 6 | compileSdkVersion 30 7 | buildToolsVersion "30.0.2" 8 | 9 | defaultConfig { 10 | applicationId "osp.leobert.android.maatsample" 11 | minSdkVersion 21 12 | targetSdkVersion 30 13 | versionCode 1 14 | versionName "1.0" 15 | 16 | testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" 17 | } 18 | 19 | buildTypes { 20 | release { 21 | minifyEnabled false 22 | proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' 23 | } 24 | } 25 | } 26 | 27 | dependencies { 28 | implementation fileTree(dir: "libs", include: ["*.jar"]) 29 | implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version" 30 | implementation 'androidx.core:core-ktx:1.3.1' 31 | implementation 'androidx.appcompat:appcompat:1.2.0' 32 | implementation 'androidx.constraintlayout:constraintlayout:2.0.1' 33 | testImplementation 'junit:junit:4.12' 34 | androidTestImplementation 'androidx.test.ext:junit:1.1.2' 35 | androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0' 36 | 37 | implementation project(':maat') 38 | 39 | } -------------------------------------------------------------------------------- /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 -------------------------------------------------------------------------------- /app/src/androidTest/java/osp/leobert/android/maatsample/ExampleInstrumentedTest.kt: -------------------------------------------------------------------------------- 1 | package osp.leobert.android.maatsample 2 | 3 | import androidx.test.platform.app.InstrumentationRegistry 4 | import androidx.test.ext.junit.runners.AndroidJUnit4 5 | 6 | import org.junit.Test 7 | import org.junit.runner.RunWith 8 | 9 | import org.junit.Assert.* 10 | 11 | /** 12 | * Instrumented test, which will execute on an Android device. 13 | * 14 | * See [testing documentation](http://d.android.com/tools/testing). 15 | */ 16 | @RunWith(AndroidJUnit4::class) 17 | class ExampleInstrumentedTest { 18 | @Test 19 | fun useAppContext() { 20 | // Context of the app under test. 21 | val appContext = InstrumentationRegistry.getInstrumentation().targetContext 22 | assertEquals("osp.leobert.android.maat", appContext.packageName) 23 | } 24 | } -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /app/src/main/java/osp/leobert/android/maatsample/App.kt: -------------------------------------------------------------------------------- 1 | package osp.leobert.android.maatsample 2 | 3 | import android.app.Application 4 | import android.os.Looper 5 | import android.util.Log 6 | import kotlinx.coroutines.CoroutineDispatcher 7 | import kotlinx.coroutines.Dispatchers 8 | import osp.leobert.android.maat.JOB 9 | import osp.leobert.android.maat.JobChuckDispatcher 10 | import osp.leobert.android.maat.Maat 11 | 12 | /** 13 | *

Package: osp.leobert.android.maatsample

14 | *

Project: Maat

15 | *

Classname: App

16 | *

Description: TODO

17 | * Created by leobert on 2020/10/24. 18 | */ 19 | class App : Application() { 20 | override fun onCreate() { 21 | super.onCreate() 22 | 23 | val maat = Maat.init(application = this, printChunkMax = 10, 24 | logger = object : Maat.Logger() { 25 | override val enable: Boolean = true 26 | 27 | 28 | override fun log(msg: String, throws: Throwable?) { 29 | Log.d("maat", "\n" + msg, throws) 30 | } 31 | 32 | }, callback = Maat.Callback(onSuccess = { 33 | Log.d("maat", "all job success") 34 | }, onFailure = { maat, job, throwable -> 35 | 36 | 37 | }), dispatcher = JobChuckDispatcher() 38 | ) 39 | 40 | maat 41 | .append(object : JOB() { 42 | override val uniqueKey: String = "chunk1_1" 43 | override val dependsOn: List = emptyList() 44 | override val dispatcher: CoroutineDispatcher = Dispatchers.IO 45 | override val emitSuccessAfterInitBlock: Boolean = false 46 | override fun init(maat: Maat) { 47 | Log.e( 48 | "maat", 49 | "run:" + uniqueKey + " isMain:" + (Looper.getMainLooper() == Looper.myLooper()) 50 | ) 51 | MockAsync.init() { 52 | Log.e( 53 | "maat", 54 | "run:" + uniqueKey + "异步完成 isMain:" + (Looper.getMainLooper() == Looper.myLooper()) 55 | ) 56 | emitSuccessAfterInit() 57 | } 58 | } 59 | 60 | override fun toString(): String { 61 | return uniqueKey 62 | } 63 | 64 | }) 65 | .append(object : JOB() { 66 | override val uniqueKey: String = "chunk1_2" 67 | override val dependsOn: List = emptyList() 68 | override val dispatcher: CoroutineDispatcher = Dispatchers.IO 69 | override val emitSuccessAfterInitBlock: Boolean = false 70 | override fun init(maat: Maat) { 71 | Log.e( 72 | "maat", 73 | "run:" + uniqueKey + " isMain:" + (Looper.getMainLooper() == Looper.myLooper()) 74 | ) 75 | MockAsync.init() { 76 | Log.e( 77 | "maat", 78 | "run:" + uniqueKey + "异步完成 isMain:" + (Looper.getMainLooper() == Looper.myLooper()) 79 | ) 80 | emitSuccessAfterInit() 81 | } 82 | //test exception 83 | // throw NullPointerException("just a test") 84 | } 85 | 86 | override fun toString(): String { 87 | return uniqueKey 88 | } 89 | 90 | }) 91 | .append(object : JOB() { 92 | override val uniqueKey: String = "a" 93 | override val dependsOn: List = emptyList() 94 | override val dispatcher: CoroutineDispatcher = Dispatchers.IO 95 | override val emitSuccessAfterInitBlock: Boolean = false 96 | override fun init(maat: Maat) { 97 | Log.e( 98 | "maat", 99 | "run:" + uniqueKey + " isMain:" + (Looper.getMainLooper() == Looper.myLooper()) 100 | ) 101 | MockAsync.init() { 102 | Log.e( 103 | "maat", 104 | "run:" + uniqueKey + "异步完成 isMain:" + (Looper.getMainLooper() == Looper.myLooper()) 105 | ) 106 | emitSuccessAfterInit() 107 | } 108 | //test exception 109 | // throw NullPointerException("just a test") 110 | } 111 | 112 | override fun toString(): String { 113 | return uniqueKey 114 | } 115 | 116 | }).append(object : JOB() { 117 | override val uniqueKey: String = "b" 118 | override val dependsOn: List = arrayListOf("a") 119 | override val dispatcher: CoroutineDispatcher = Dispatchers.Main /* + Job()*/ 120 | 121 | override fun init(maat: Maat) { 122 | Log.e( 123 | "maat", 124 | "run:" + uniqueKey + " isMain:" + (Looper.getMainLooper() == Looper.myLooper()) 125 | ) 126 | //模拟误操作1:同步初始化,但是调用了触发: 并不会出问题 127 | emitSuccessAfterInit() 128 | } 129 | 130 | override fun toString(): String { 131 | return uniqueKey 132 | } 133 | 134 | }).append(object : JOB() { 135 | override val uniqueKey: String = "c" 136 | override val dependsOn: List = arrayListOf("a") 137 | override val dispatcher: CoroutineDispatcher = Dispatchers.IO /* + Job()*/ 138 | override val emitSuccessAfterInitBlock: Boolean = false 139 | 140 | override fun init(maat: Maat) { 141 | Log.e( 142 | "maat", 143 | "run:" + uniqueKey + " isMain:" + (Looper.getMainLooper() == Looper.myLooper()) 144 | ) 145 | //模拟误操作2:多次触发: 146 | emitSuccessAfterInit() 147 | emitSuccessAfterInit() //已经释放,并不会有 148 | } 149 | 150 | override fun toString(): String { 151 | return uniqueKey 152 | } 153 | 154 | }) 155 | .append(object : JOB() { 156 | override val uniqueKey: String = "b" 157 | override val dependsOn: List = arrayListOf("chunk1_1") 158 | override val dispatcher: CoroutineDispatcher = Dispatchers.IO /* + Job()*/ 159 | override val emitSuccessAfterInitBlock: Boolean = false 160 | 161 | override fun init(maat: Maat) { 162 | Log.e( 163 | "maat", 164 | "run:" + uniqueKey + " isMain:" + (Looper.getMainLooper() == Looper.myLooper()) 165 | ) 166 | //模拟误操作2:多次触发: 167 | emitSuccessAfterInit() 168 | } 169 | 170 | override fun toString(): String { 171 | return uniqueKey 172 | } 173 | 174 | }) 175 | .append(object : JOB() { 176 | override val uniqueKey: String = "a_chunk2_1" 177 | override val dependsOn: List = arrayListOf("a") 178 | override val dispatcher: CoroutineDispatcher = Dispatchers.IO /* + Job()*/ 179 | override val emitSuccessAfterInitBlock: Boolean = false 180 | 181 | override fun init(maat: Maat) { 182 | Log.e( 183 | "maat", 184 | "run:" + uniqueKey + " isMain:" + (Looper.getMainLooper() == Looper.myLooper()) 185 | ) 186 | //模拟误操作2:多次触发: 187 | emitSuccessAfterInit() 188 | } 189 | 190 | override fun toString(): String { 191 | return uniqueKey 192 | } 193 | 194 | }) 195 | .append(object : JOB() { 196 | override val uniqueKey: String = "a_chunk2_2" 197 | override val dependsOn: List = arrayListOf("a") 198 | override val dispatcher: CoroutineDispatcher = Dispatchers.IO /* + Job()*/ 199 | override val emitSuccessAfterInitBlock: Boolean = false 200 | 201 | override fun init(maat: Maat) { 202 | Log.e( 203 | "maat", 204 | "run:" + uniqueKey + " isMain:" + (Looper.getMainLooper() == Looper.myLooper()) 205 | ) 206 | //模拟误操作2:多次触发: 207 | emitSuccessAfterInit() 208 | } 209 | 210 | override fun toString(): String { 211 | return uniqueKey 212 | } 213 | 214 | }) 215 | 216 | 217 | .append(object : JOB() { 218 | override val uniqueKey: String = "d" 219 | override val dependsOn: List = arrayListOf("a", "b", "c") 220 | override val dispatcher: CoroutineDispatcher = Dispatchers.Main 221 | 222 | override fun init(maat: Maat) { 223 | Log.e( 224 | "maat", 225 | "run:" + uniqueKey + " isMain:" + (Looper.getMainLooper() == Looper.myLooper()) 226 | ) 227 | } 228 | 229 | override fun toString(): String { 230 | return uniqueKey 231 | } 232 | 233 | }).start() 234 | } 235 | } -------------------------------------------------------------------------------- /app/src/main/java/osp/leobert/android/maatsample/MainActivity.kt: -------------------------------------------------------------------------------- 1 | package osp.leobert.android.maatsample 2 | 3 | import android.os.Bundle 4 | import androidx.appcompat.app.AppCompatActivity 5 | 6 | class MainActivity : AppCompatActivity() { 7 | override fun onCreate(savedInstanceState: Bundle?) { 8 | super.onCreate(savedInstanceState) 9 | setContentView(R.layout.activity_main) 10 | 11 | } 12 | } -------------------------------------------------------------------------------- /app/src/main/java/osp/leobert/android/maatsample/MockAsync.kt: -------------------------------------------------------------------------------- 1 | package osp.leobert.android.maatsample 2 | 3 | import android.util.Log 4 | import kotlinx.coroutines.* 5 | import osp.leobert.android.maat.MaatUtil 6 | 7 | /** 8 | *

Package: osp.leobert.android.maatsample

9 | *

Project: Maat

10 | *

Classname: MockAsync

11 | * Created by leobert on 2020/10/24. 12 | */ 13 | object MockAsync { 14 | 15 | fun init(callback: () -> Unit) { 16 | CoroutineScope(Dispatchers.Main + Job()).launch { 17 | withContext(Dispatchers.Default) { 18 | Log.e("maat", "模拟子线程异步初始化,isMain:${MaatUtil.isMainThread()},${System.currentTimeMillis()}") 19 | delay(200) 20 | } 21 | Log.e("maat", "模拟加载完成,主线程通知加载完成,isMain:${MaatUtil.isMainThread()},${System.currentTimeMillis()}") 22 | 23 | callback.invoke() 24 | } 25 | } 26 | } -------------------------------------------------------------------------------- /app/src/main/res/drawable-v24/ic_launcher_foreground.xml: -------------------------------------------------------------------------------- 1 | 7 | 8 | 9 | 15 | 18 | 21 | 22 | 23 | 24 | 30 | -------------------------------------------------------------------------------- /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_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 8 | 9 | 17 | 18 | -------------------------------------------------------------------------------- /app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /app/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /app/src/main/res/mipmap-hdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/leobert-lan/Maat/c4c1a37a9d68e2c65624de4ee796803359b5d074/app/src/main/res/mipmap-hdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-hdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/leobert-lan/Maat/c4c1a37a9d68e2c65624de4ee796803359b5d074/app/src/main/res/mipmap-hdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-mdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/leobert-lan/Maat/c4c1a37a9d68e2c65624de4ee796803359b5d074/app/src/main/res/mipmap-mdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-mdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/leobert-lan/Maat/c4c1a37a9d68e2c65624de4ee796803359b5d074/app/src/main/res/mipmap-mdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/leobert-lan/Maat/c4c1a37a9d68e2c65624de4ee796803359b5d074/app/src/main/res/mipmap-xhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/leobert-lan/Maat/c4c1a37a9d68e2c65624de4ee796803359b5d074/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/leobert-lan/Maat/c4c1a37a9d68e2c65624de4ee796803359b5d074/app/src/main/res/mipmap-xxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/leobert-lan/Maat/c4c1a37a9d68e2c65624de4ee796803359b5d074/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/leobert-lan/Maat/c4c1a37a9d68e2c65624de4ee796803359b5d074/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/leobert-lan/Maat/c4c1a37a9d68e2c65624de4ee796803359b5d074/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/values/colors.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | #6200EE 4 | #3700B3 5 | #03DAC5 6 | -------------------------------------------------------------------------------- /app/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | Maat 3 | -------------------------------------------------------------------------------- /app/src/main/res/values/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 9 | 10 | -------------------------------------------------------------------------------- /app/src/test/java/osp/leobert/android/maatsample/ExampleUnitTest.kt: -------------------------------------------------------------------------------- 1 | package osp.leobert.android.maatsample 2 | 3 | import org.junit.Test 4 | 5 | import org.junit.Assert.* 6 | 7 | /** 8 | * Example local unit test, which will execute on the development machine (host). 9 | * 10 | * See [testing documentation](http://d.android.com/tools/testing). 11 | */ 12 | class ExampleUnitTest { 13 | @Test 14 | fun addition_isCorrect() { 15 | assertEquals(4, 2 + 2) 16 | } 17 | } -------------------------------------------------------------------------------- /bintrayv1.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.jfrog.bintray' 2 | 3 | version = libraryVersion 4 | 5 | if (project.hasProperty("android")) { // Android libraries 6 | task sourcesJar(type: Jar) { 7 | classifier = 'sources' 8 | from android.sourceSets.main.java.srcDirs 9 | } 10 | 11 | // task javadoc(type: Javadoc) { 12 | // options.encoding "UTF-8" 13 | // options.charSet 'UTF-8' 14 | // source = android.sourceSets.main.java.srcDirs 15 | // classpath += project.files(android.getBootClasspath().join(File.pathSeparator)) 16 | // } 17 | 18 | task javadoc(type: Javadoc) { 19 | options { 20 | encoding("utf-8") 21 | links "http://docs.oracle.com/javase/7/docs/api" 22 | } 23 | exclude("**/*.kt") 24 | source = android.sourceSets.main.java.srcDirs 25 | classpath += project.files(android.getBootClasspath().join(File.pathSeparator)) 26 | } 27 | } else { // Java libraries 28 | task sourcesJar(type: Jar, dependsOn: classes) { 29 | classifier = 'sources' 30 | from sourceSets.main.allSource 31 | } 32 | } 33 | 34 | task javadocJar(type: Jar, dependsOn: javadoc) { 35 | classifier = 'javadoc' 36 | from javadoc.destinationDir 37 | } 38 | 39 | artifacts { 40 | archives javadocJar 41 | archives sourcesJar 42 | } 43 | 44 | // Bintray 45 | Properties properties = new Properties() 46 | properties.load(project.rootProject.file('local.properties').newDataInputStream()) 47 | 48 | bintray { 49 | user = properties.getProperty("bintray.user") 50 | key = properties.getProperty("bintray.apikey") 51 | 52 | configurations = ['archives'] 53 | pkg { 54 | repo = bintrayRepo 55 | name = bintrayName 56 | desc = libraryDescription 57 | websiteUrl = siteUrl 58 | vcsUrl = gitUrl 59 | licenses = allLicenses 60 | publish = true 61 | publicDownloadNumbers = true 62 | version { 63 | desc = libraryDescription 64 | gpg { 65 | sign = true //Determines whether to GPG sign the files. The default is false 66 | passphrase = properties.getProperty("bintray.gpg.password") 67 | //Optional. The passphrase for GPG signing' 68 | } 69 | } 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /build.gradle: -------------------------------------------------------------------------------- 1 | // Top-level build file where you can add configuration options common to all sub-projects/modules. 2 | buildscript { 3 | ext.kotlin_version = "1.3.72" 4 | repositories { 5 | google() 6 | jcenter() 7 | } 8 | dependencies { 9 | classpath "com.android.tools.build:gradle:4.0.1" 10 | classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" 11 | 12 | // NOTE: Do not place your application dependencies here; they belong 13 | // in the individual module build.gradle files 14 | classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.2' 15 | classpath 'com.github.dcendents:android-maven-gradle-plugin:2.1' 16 | } 17 | } 18 | 19 | allprojects { 20 | repositories { 21 | google() 22 | jcenter() 23 | } 24 | } 25 | 26 | task clean(type: Delete) { 27 | delete rootProject.buildDir 28 | } -------------------------------------------------------------------------------- /gradle.properties: -------------------------------------------------------------------------------- 1 | # Project-wide Gradle settings. 2 | # IDE (e.g. Android Studio) users: 3 | # Gradle settings configured through the IDE *will override* 4 | # any settings specified in this file. 5 | # For more details on how to configure your build environment visit 6 | # http://www.gradle.org/docs/current/userguide/build_environment.html 7 | # Specifies the JVM arguments used for the daemon process. 8 | # The setting is particularly useful for tweaking memory settings. 9 | org.gradle.jvmargs=-Xmx2048m 10 | # When configured, Gradle will run in incubating parallel mode. 11 | # This option should only be used with decoupled projects. More details, visit 12 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects 13 | # org.gradle.parallel=true 14 | # AndroidX package structure to make it clearer which packages are bundled with the 15 | # Android operating system, and which are packaged with your app"s APK 16 | # https://developer.android.com/topic/libraries/support-library/androidx-rn 17 | android.useAndroidX=true 18 | # Automatically convert third-party libraries to use AndroidX 19 | android.enableJetifier=true 20 | # Kotlin code style for this project: "official" or "obsolete": 21 | kotlin.code.style=official 22 | bintrayRepo=maven 23 | publishedGroupId=osp.leobert.android 24 | siteUrl=https://github.com/leobert-lan/Maat 25 | gitUrl=https://github.com/leobert-lan/Maat.git 26 | developerId=leobert_lan 27 | developerName=leobert 28 | developerEmail=leobert.l@hotmail.com -------------------------------------------------------------------------------- /installv1.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.github.dcendents.android-maven' 2 | 3 | group = publishedGroupId // Maven Group ID for the artifact 4 | 5 | install { 6 | repositories.mavenInstaller { 7 | // This generates POM.xml with proper parameters 8 | pom { 9 | project { 10 | packaging 'aar' 11 | groupId publishedGroupId 12 | artifactId artifact 13 | 14 | // Add your description here 15 | name libraryName 16 | description libraryDescription 17 | url siteUrl 18 | 19 | // Set your license 20 | licenses { 21 | license { 22 | name licenseName 23 | url licenseUrl 24 | } 25 | } 26 | developers { 27 | developer { 28 | id developerId 29 | name developerName 30 | email developerEmail 31 | } 32 | } 33 | scm { 34 | connection gitUrl 35 | developerConnection gitUrl 36 | url siteUrl 37 | 38 | } 39 | } 40 | } 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /maat/.gitignore: -------------------------------------------------------------------------------- 1 | /build -------------------------------------------------------------------------------- /maat/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.library' 2 | apply plugin: 'kotlin-android' 3 | apply plugin: 'kotlin-android-extensions' 4 | 5 | android { 6 | compileSdkVersion 30 7 | buildToolsVersion "30.0.2" 8 | compileOptions { 9 | sourceCompatibility JavaVersion.VERSION_1_8 10 | targetCompatibility JavaVersion.VERSION_1_8 11 | } 12 | 13 | defaultConfig { 14 | minSdkVersion 21 15 | targetSdkVersion 30 16 | versionCode 1 17 | versionName "1.0" 18 | 19 | testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" 20 | consumerProguardFiles "consumer-rules.pro" 21 | } 22 | 23 | buildTypes { 24 | release { 25 | minifyEnabled false 26 | proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' 27 | } 28 | } 29 | } 30 | 31 | dependencies { 32 | implementation fileTree(dir: "libs", include: ["*.jar"]) 33 | implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version" 34 | implementation 'androidx.core:core-ktx:1.3.2' 35 | implementation 'androidx.appcompat:appcompat:1.2.0' 36 | testImplementation 'junit:junit:4.12' 37 | androidTestImplementation 'androidx.test.ext:junit:1.1.2' 38 | androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0' 39 | api 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.3.6' 40 | 41 | } 42 | 43 | //=======// 44 | 45 | 46 | def MAVEN_LOCAL_PATH = uri('../../local_repo') 47 | def ARTIFACT_ID = 'maat' 48 | def VERSION_NAME = '1.1.1' 49 | def GROUP_ID = 'osp.leobert.android' 50 | 51 | ext { 52 | bintrayRepo = 'maven' 53 | bintrayName = ARTIFACT_ID 54 | artifact = ARTIFACT_ID 55 | libraryName = ARTIFACT_ID 56 | libraryVersion = VERSION_NAME 57 | libraryDescription = 'gradle plugin for buid component' 58 | licenseName = 'The Apache Software License, Version 2.0' 59 | licenseUrl = 'http://www.apache.org/licenses/LICENSE-2.0.txt' 60 | allLicenses = ["Apache-2.0"] 61 | } 62 | apply from: '../bintrayv1.gradle' 63 | 64 | 65 | apply from: '../installv1.gradle' 66 | //apply plugin: 'maven' 67 | //uploadArchives() { 68 | // repositories { 69 | // mavenDeployer { 70 | // repository(url: MAVEN_LOCAL_PATH) 71 | // pom.project { 72 | // groupId GROUP_ID 73 | // artifactId ARTIFACT_ID 74 | // version VERSION_NAME 75 | // } 76 | // } 77 | // } 78 | //} 79 | 80 | -------------------------------------------------------------------------------- /maat/consumer-rules.pro: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/leobert-lan/Maat/c4c1a37a9d68e2c65624de4ee796803359b5d074/maat/consumer-rules.pro -------------------------------------------------------------------------------- /maat/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 -------------------------------------------------------------------------------- /maat/src/androidTest/java/osp/leobert/android/maat/ExampleInstrumentedTest.kt: -------------------------------------------------------------------------------- 1 | package osp.leobert.android.maat 2 | 3 | import android.os.Looper 4 | import android.util.Log 5 | import androidx.test.ext.junit.runners.AndroidJUnit4 6 | import androidx.test.platform.app.InstrumentationRegistry 7 | import kotlinx.coroutines.CoroutineDispatcher 8 | import kotlinx.coroutines.Dispatchers 9 | import org.junit.Test 10 | import org.junit.runner.RunWith 11 | 12 | /** 13 | * Instrumented test, which will execute on an Android device. 14 | * 15 | * See [testing documentation](http://d.android.com/tools/testing). 16 | */ 17 | @RunWith(AndroidJUnit4::class) 18 | class ExampleInstrumentedTest { 19 | @Test 20 | fun useAppContext() { 21 | // Context of the app under test. 22 | val appContext = InstrumentationRegistry.getInstrumentation().targetContext 23 | // assertEquals("osp.leobert.android.maat.test", appContext.packageName) 24 | 25 | val maat = Maat(appContext.applicationContext, 6, object : Maat.Logger() { 26 | override val enable: Boolean = true 27 | 28 | 29 | override fun log(msg: String, throws: Throwable?) { 30 | // println(msg) 31 | Log.d("maat", msg, throws) 32 | } 33 | 34 | }) 35 | print(Thread.currentThread().name) 36 | maat.append(object : JOB() { 37 | override val uniqueKey: String = "a" 38 | override val dependsOn: List = emptyList() 39 | override val dispatcher: CoroutineDispatcher = Dispatchers.IO 40 | 41 | override fun init(maat: Maat) { 42 | Log.e( 43 | "maat", 44 | "run:" + uniqueKey + " isMain:" + (Looper.getMainLooper() == Looper.myLooper()) 45 | ) 46 | } 47 | 48 | override fun toString(): String { 49 | return uniqueKey 50 | } 51 | 52 | }).append(object : JOB() { 53 | override val uniqueKey: String = "b" 54 | override val dependsOn: List = arrayListOf("a") 55 | override val dispatcher: CoroutineDispatcher = Dispatchers.Main 56 | 57 | override fun init(maat: Maat) { 58 | Log.e( 59 | "maat", 60 | "run:" + uniqueKey + " isMain:" + (Looper.getMainLooper() == Looper.myLooper()) 61 | ) 62 | } 63 | 64 | override fun toString(): String { 65 | return uniqueKey 66 | } 67 | 68 | }).append(object : JOB() { 69 | override val uniqueKey: String = "c" 70 | override val dependsOn: List = arrayListOf("a") 71 | override val dispatcher: CoroutineDispatcher = Dispatchers.IO 72 | 73 | override fun init(maat: Maat) { 74 | Log.e( 75 | "maat", 76 | "run:" + uniqueKey + " isMain:" + (Looper.getMainLooper() == Looper.myLooper()) 77 | ) 78 | } 79 | 80 | override fun toString(): String { 81 | return uniqueKey 82 | } 83 | 84 | }).append(object : JOB() { 85 | override val uniqueKey: String = "d" 86 | override val dependsOn: List = arrayListOf("a", "b", "c") 87 | override val dispatcher: CoroutineDispatcher = Dispatchers.Main 88 | 89 | override fun init(maat: Maat) { 90 | Log.e( 91 | "maat", 92 | "run:" + uniqueKey + " isMain:" + (Looper.getMainLooper() == Looper.myLooper()) 93 | ) 94 | } 95 | 96 | override fun toString(): String { 97 | return uniqueKey 98 | } 99 | 100 | }).start() 101 | } 102 | } -------------------------------------------------------------------------------- /maat/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 3 | 4 | / 5 | -------------------------------------------------------------------------------- /maat/src/main/java/osp/leobert/android/maat/JOB.kt: -------------------------------------------------------------------------------- 1 | package osp.leobert.android.maat 2 | 3 | import kotlinx.coroutines.CoroutineDispatcher 4 | import kotlinx.coroutines.Dispatchers 5 | import kotlinx.coroutines.MainScope 6 | import kotlinx.coroutines.flow.catch 7 | import kotlinx.coroutines.flow.collect 8 | import kotlinx.coroutines.flow.flow 9 | import kotlinx.coroutines.flow.flowOn 10 | import kotlinx.coroutines.launch 11 | 12 | /** 13 | *

Package: osp.leobert.android.maat

14 | *

Classname: Job

15 | * Created by leobert on 2020/9/24. 16 | */ 17 | abstract class JOB { 18 | abstract val uniqueKey: String 19 | 20 | abstract val dependsOn: List 21 | 22 | abstract val dispatcher: CoroutineDispatcher 23 | 24 | private var finishEmitter: FinishEmitter? = null 25 | 26 | open val emitSuccessAfterInitBlock = true 27 | 28 | 29 | internal fun runInit(maat: Maat) { 30 | MainScope().launch { 31 | 32 | flow { 33 | if (!emitSuccessAfterInitBlock) 34 | finishEmitter = FinishEmitter(maat) 35 | init(maat) 36 | if (emitSuccessAfterInitBlock) 37 | emit(true) 38 | }.flowOn(dispatcher) 39 | .catch { 40 | maat.onJobFailed(this@JOB, it) 41 | }.flowOn(Dispatchers.Main) 42 | .collect { 43 | maat.onJobSuccess(this@JOB) 44 | } 45 | } 46 | } 47 | 48 | protected fun emitSuccessAfterInit() { 49 | MainScope().launch { 50 | finishEmitter?.emit() 51 | } 52 | } 53 | 54 | abstract fun init(maat: Maat) 55 | 56 | inner class FinishEmitter(val maat: Maat) { 57 | var hasEmit = false 58 | 59 | @Synchronized 60 | fun emit() { 61 | if (hasEmit) { 62 | Maat.getDefault().logger.log( 63 | "not allowed to emit success twice!", 64 | Throwable("from $uniqueKey") 65 | ) 66 | return 67 | } 68 | hasEmit = true 69 | 70 | maat.onJobSuccess(this@JOB) 71 | finishEmitter = null 72 | } 73 | 74 | } 75 | 76 | } -------------------------------------------------------------------------------- /maat/src/main/java/osp/leobert/android/maat/JobChunk.kt: -------------------------------------------------------------------------------- 1 | package osp.leobert.android.maat 2 | 3 | /** 4 | *

Package: osp.leobert.android.maat

5 | *

Classname: JobChunk

6 | * Created by leobert on 2020/9/24. 7 | */ 8 | class JobChunk private constructor() { 9 | 10 | companion object { 11 | fun head() = JobChunk().apply { 12 | this.current = this 13 | } 14 | 15 | fun chunk() = JobChunk() 16 | 17 | fun JobChunk.append(): JobChunk { 18 | return chunk().apply { 19 | this@append.nextChunk = this 20 | } 21 | } 22 | 23 | fun JobChunk.info(): String { 24 | return buildString { 25 | append(this@info.jobs).append(", ") 26 | this@info.nextChunk?.info()?.let { append(it) } 27 | } 28 | } 29 | } 30 | 31 | private val jobs: LinkedHashSet = LinkedHashSet() 32 | 33 | private var current: JobChunk? = null 34 | var nextChunk: JobChunk? = null 35 | 36 | private val onHandlingJobsKey: HashSet = hashSetOf() 37 | 38 | @Synchronized 39 | fun markOnHandling(key:String) { 40 | current?.onHandlingJobsKey?.add(key) 41 | } 42 | 43 | @Synchronized 44 | fun markHandled(key:String) { 45 | current?.onHandlingJobsKey?.remove(key) 46 | } 47 | 48 | @Synchronized 49 | fun haveNextInChunk(): Boolean = current?.jobs?.isNotEmpty() ?: false 50 | 51 | @Synchronized 52 | fun hasHandingJobsInChunk(): Boolean = current?.onHandlingJobsKey?.isNotEmpty() ?: false 53 | 54 | @Synchronized 55 | fun haveNext(): Boolean = current?.run { haveNext(this) } ?: false 56 | 57 | 58 | private fun haveNext(chunk: JobChunk): Boolean { 59 | return chunk.jobs.isNotEmpty() || (chunk.nextChunk?.run { haveNext(this) } ?: false) 60 | } 61 | 62 | @Synchronized 63 | fun next(): JOB? { 64 | synchronized(this) { 65 | if (haveNext()) { 66 | if (current?.jobs.isNullOrEmpty()) { 67 | current = current?.nextChunk 68 | } 69 | return current?.jobs?.firstOrNull()?.apply { 70 | current?.jobs?.remove(this) 71 | } 72 | } 73 | return null 74 | } 75 | } 76 | 77 | @Synchronized 78 | fun move2NextChunk(): JobChunk? { 79 | current = current?.nextChunk 80 | return current 81 | } 82 | 83 | internal fun addJobs(jobs: List): JobChunk { 84 | this.jobs.addAll(jobs) 85 | return this 86 | } 87 | 88 | internal fun addJob(job: JOB): JobChunk { 89 | this.jobs.add(job) 90 | return this 91 | } 92 | } -------------------------------------------------------------------------------- /maat/src/main/java/osp/leobert/android/maat/Maat.kt: -------------------------------------------------------------------------------- 1 | package osp.leobert.android.maat 2 | 3 | import android.app.Application 4 | import androidx.annotation.MainThread 5 | import kotlinx.coroutines.CoroutineDispatcher 6 | import kotlinx.coroutines.Dispatchers 7 | import osp.leobert.android.maat.JobChunk.Companion.append 8 | import osp.leobert.android.maat.JobChunk.Companion.info 9 | import osp.leobert.android.maat.dag.DAG 10 | import osp.leobert.android.maat.dag.Edge 11 | import osp.leobert.android.maat.dag.Type 12 | import java.util.* 13 | import kotlin.collections.HashMap 14 | import kotlin.collections.component1 15 | import kotlin.collections.component2 16 | import kotlin.collections.set 17 | 18 | /** 19 | *

Package: osp.leobert.android.maat

20 | *

Classname: Maat

21 | * Created by leobert on 2020/9/23. 22 | */ 23 | class Maat( 24 | val application: Application, private val printChunkMax: Int, 25 | internal val logger: Logger, internal val callback: Callback? = null, 26 | internal val dispatcher: Dispatcher = JobDispatcher() 27 | ) { 28 | 29 | abstract class Logger { 30 | abstract val enable: Boolean 31 | abstract fun log(msg: String, throws: Throwable? = null) 32 | } 33 | 34 | class Callback(val onSuccess: (Maat) -> Unit, val onFailure: (Maat, JOB, Throwable) -> Unit) 35 | 36 | companion object { 37 | private var sInstance: Maat? = null 38 | 39 | @Synchronized 40 | @JvmOverloads 41 | fun init( 42 | application: Application, printChunkMax: Int, 43 | logger: Logger, callback: Callback? = null, 44 | dispatcher: Dispatcher = JobDispatcher() 45 | 46 | ): Maat { 47 | 48 | synchronized(Maat::class) { 49 | val tmp = sInstance 50 | if (tmp == null) { 51 | synchronized(Maat::class) { 52 | val s = Maat(application, printChunkMax, logger, callback, dispatcher) 53 | sInstance = s 54 | return s 55 | } 56 | } else { 57 | return tmp 58 | } 59 | } 60 | } 61 | 62 | @Synchronized 63 | fun getDefault(): Maat { 64 | return sInstance ?: throw MaatException("must call init at first") 65 | } 66 | 67 | @Synchronized 68 | @Throws(MaatException::class) 69 | fun release() { 70 | if (getDefault().hasFinished()) { 71 | sInstance = null 72 | return 73 | } 74 | 75 | throw MaatException("maat has not finished it's job") 76 | } 77 | 78 | 79 | private fun DAG.bfs(): JobChunk { 80 | 81 | val zeroDeque = ArrayDeque() 82 | val inDegrees = HashMap().apply { 83 | putAll(this@bfs.inDegreeCache) 84 | } 85 | inDegrees.forEach { (v, d) -> 86 | if (d == 0) 87 | zeroDeque.offer(v) 88 | } 89 | 90 | val head = JobChunk.head() 91 | var currentChunk = head 92 | 93 | val tmpDeque = ArrayDeque() 94 | 95 | while (zeroDeque.isNotEmpty() || tmpDeque.isNotEmpty()) { 96 | if (zeroDeque.isEmpty()) { 97 | currentChunk = currentChunk.append() 98 | zeroDeque.addAll(tmpDeque) 99 | tmpDeque.clear() 100 | } 101 | zeroDeque.poll()?.let { vertex -> 102 | currentChunk.addJob(vertex) 103 | 104 | this.getEdgeContainsPoint(vertex, Type.X).forEach { edge -> 105 | inDegrees[edge.to] = (inDegrees[edge.to] ?: 0).minus(edge.weight).apply { 106 | if (this == 0) 107 | tmpDeque.offer(edge.to) 108 | } 109 | } 110 | } 111 | } 112 | return head 113 | } 114 | } 115 | 116 | private val dag: DAG = DAG({ job -> job.uniqueKey }, printChunkMax) 117 | private val start = arrayListOf("start") 118 | private val startJob = object : JOB() { 119 | override val uniqueKey: String = "start" 120 | override val dependsOn: List = emptyList() 121 | override val dispatcher: CoroutineDispatcher = Dispatchers.Main 122 | 123 | override fun init(maat: Maat) { 124 | } 125 | 126 | override fun toString(): String { 127 | return uniqueKey 128 | } 129 | 130 | } 131 | 132 | private val allJobsCache = hashMapOf() 133 | private val allJobsNameCache = linkedSetOf() 134 | private val allDependsOnCache = hashSetOf() 135 | private var currentJobChunk: JobChunk? = null 136 | 137 | fun append(job: JOB): Maat { 138 | allJobsCache[job.uniqueKey] = job 139 | allJobsNameCache.add(job.uniqueKey) 140 | allDependsOnCache.addAll(job.dependsOn) 141 | return this 142 | } 143 | 144 | @Throws(MaatException::class) 145 | fun start() { 146 | allJobsNameCache.forEach { allDependsOnCache.remove(it) } 147 | allDependsOnCache.takeIf { it.isNotEmpty() } 148 | ?.let { throw MaatException("missing jobs:$it") } 149 | 150 | //below 24 151 | for (entry in allJobsCache) { 152 | entry.value.let { job -> 153 | (job.dependsOn.takeIf { it.isNotEmpty() } ?: start).forEach { 154 | dag.addEdge(Edge(allJobsCache[it] ?: startJob, job)) 155 | } 156 | } 157 | } 158 | 159 | if (logger.enable) { 160 | logger.log(dag.debugMatrix()) 161 | } 162 | 163 | dag.recursive(startJob, arrayListOf()) 164 | if (dag.loopbackList.isNotEmpty()) { 165 | throw MaatException("cycle exist:${dag.loopbackList}") 166 | } 167 | 168 | if (logger.enable) { 169 | logger.log("全部路径:") 170 | dag.deepPathList.forEach { 171 | logger.log(it.toString()) 172 | } 173 | } 174 | 175 | currentJobChunk = createJobChunk() 176 | 177 | if (logger.enable) 178 | logger.log("init order: ${currentJobChunk?.info()}") 179 | 180 | dispatcher.start(this, currentJobChunk) 181 | } 182 | 183 | @MainThread 184 | fun onJobFailed(job: JOB, throws: Throwable) { 185 | if (logger.enable) 186 | logger.log( 187 | "onJobFailed:${job.uniqueKey}, called on MainThread:${MaatUtil.isMainThread()}", 188 | throws 189 | ) 190 | 191 | callback?.onFailure?.invoke(this, job, throws) 192 | } 193 | 194 | @MainThread 195 | internal fun onJobSuccess(job: JOB) { 196 | dispatcher.dispatchOnJobSuccess(this, currentJobChunk, job) 197 | } 198 | 199 | private fun createJobChunk(): JobChunk { 200 | return dag.bfs() 201 | } 202 | 203 | fun hasFinished(): Boolean { 204 | return currentJobChunk?.haveNext() != true 205 | } 206 | } -------------------------------------------------------------------------------- /maat/src/main/java/osp/leobert/android/maat/MaatException.kt: -------------------------------------------------------------------------------- 1 | package osp.leobert.android.maat 2 | 3 | import java.lang.RuntimeException 4 | 5 | /** 6 | *

Package: osp.leobert.android.maat

7 | *

Classname: MaatException

8 | * Created by leobert on 2020/9/24. 9 | */ 10 | class MaatException(msg: String, throws: Throwable? = null) : RuntimeException(msg, throws) -------------------------------------------------------------------------------- /maat/src/main/java/osp/leobert/android/maat/MaatUtil.kt: -------------------------------------------------------------------------------- 1 | package osp.leobert.android.maat 2 | 3 | import android.os.Looper 4 | 5 | /** 6 | *

Package: osp.leobert.android.maat

7 | *

Classname: MaatUtil

8 | * Created by leobert on 2020/9/25. 9 | */ 10 | object MaatUtil { 11 | fun isMainThread():Boolean = Looper.getMainLooper() == Looper.myLooper() 12 | } -------------------------------------------------------------------------------- /maat/src/main/java/osp/leobert/android/maat/dag/DAG.kt: -------------------------------------------------------------------------------- 1 | package osp.leobert.android.maat.dag 2 | 3 | import java.util.* 4 | import kotlin.collections.HashMap 5 | import kotlin.collections.HashSet 6 | 7 | 8 | /** 9 | *

Package: osp.leobert.android.maat.dag

10 | *

Classname: DAG

11 | * Created by leobert on 2020/9/23. 12 | */ 13 | class DAG(val nameOf: (T) -> String, val printChunkMax: Int) { 14 | 15 | //记录两份网络节点,使用空间换时间,提升反向查询效率 16 | 17 | //网络初始化,x_k,y_k 18 | private val edgesByStart: MutableMap> = HashMap() 19 | 20 | //网络初始化,y_k,x_k 21 | private val edgesByEnd: MutableMap> = HashMap() 22 | 23 | //回环路径 24 | val loopbackList: MutableList = LinkedList() 25 | 26 | //节点深度路径 27 | val deepPathList: MutableList> = LinkedList() 28 | 29 | val inDegreeCache = HashMap() 30 | 31 | fun addEdge(edge: Edge) { 32 | if (!edgesByStart.containsKey(edge.from)) { 33 | edgesByStart[edge.from] = hashMapOf() 34 | } 35 | edgesByStart[edge.from]?.put(edge.to, edge.weight) 36 | 37 | if (!edgesByEnd.containsKey(edge.to)) { 38 | edgesByEnd[edge.to] = HashMap() 39 | } 40 | 41 | inDegreeCache[edge.to] = (inDegreeCache[edge.to] ?: 0).plus(edge.weight) 42 | inDegreeCache[edge.from] = inDegreeCache[edge.from]?:0 43 | 44 | edgesByEnd[edge.to]?.put(edge.from, edge.weight) 45 | } 46 | 47 | private val allPoint: Set 48 | get() = HashSet().apply { 49 | this.addAll(edgesByStart.keys) 50 | this.addAll(edgesByEnd.keys) 51 | } 52 | 53 | 54 | fun getEdgeContainsPoint(point: T, type: Type): List> { 55 | val linePointList: MutableList> = ArrayList() 56 | if (type == Type.X) { 57 | edgesByStart[point]?.forEach { 58 | linePointList.add(Edge(point, it.key, it.value)) 59 | } 60 | } else { 61 | edgesByEnd[point]?.forEach { 62 | linePointList.add(Edge(it.key, point, it.value)) 63 | } 64 | } 65 | return linePointList 66 | } 67 | 68 | private fun debugPathInfo(pathList: MutableList): String { 69 | return pathList.map { it.let(nameOf) }.toString() 70 | } 71 | 72 | fun recursive(startPoint: T, pathList: MutableList) { 73 | if (pathList.contains(startPoint)) { 74 | loopbackList.add("${debugPathInfo(pathList)}->${startPoint.let(nameOf)}") 75 | return 76 | } 77 | pathList.add(startPoint) 78 | val edgesFromStartPoint = getEdgeContainsPoint(startPoint, Type.X) 79 | if (edgesFromStartPoint.isEmpty()) { 80 | val descList: ArrayList = ArrayList(pathList.size) 81 | pathList.forEach { path -> descList.add(path) } 82 | deepPathList.add(descList) 83 | } 84 | edgesFromStartPoint.forEach { 85 | recursive(it.to, pathList) 86 | } 87 | 88 | pathList.remove(startPoint) 89 | } 90 | 91 | fun debugMatrix(): String { 92 | val placeholder = printChunkMax 93 | 94 | val placeholderString = StringBuilder() 95 | for (i in 0 until placeholder) { 96 | placeholderString.append("-") 97 | } 98 | val info = StringBuilder() 99 | val allSet: Set = allPoint 100 | 101 | info.append(String.format("%-" + placeholder + "s", "")).append(" ") 102 | for (x in allSet) { 103 | info.append(String.format("%-" + placeholder + "s", x.let(nameOf))) 104 | } 105 | info.append("\n").append(String.format("%-" + placeholder + "s", "X\\Y")).append(" ") 106 | for (ignored in allSet) { 107 | info.append(placeholderString) 108 | } 109 | 110 | info.append("\n") 111 | 112 | for (x in allSet) { 113 | info.append(String.format("%-" + placeholder + "s|", x.let(nameOf))) 114 | 115 | for (y in allSet) { 116 | val linePoints: Map? = edgesByStart[x] 117 | var degree: Int? = 0 118 | if (linePoints != null && linePoints[y] != null) { 119 | degree = linePoints[y] 120 | } 121 | info.append(String.format("%-" + placeholder + "s", (degree ?: 0).toString())) 122 | } 123 | info.append("\n") 124 | } 125 | return info.toString() 126 | } 127 | } 128 | // 129 | //fun main() { 130 | // val dag = DAG(nameOf = { it }, printChunkMax = 3) 131 | // dag.addEdge(Edge("a", "b", 1)) 132 | // dag.addEdge(Edge("a", "c", 1)) 133 | // dag.addEdge(Edge("c", "d", 1)) 134 | // dag.addEdge(Edge("b", "d", 1)) 135 | // dag.addEdge(Edge("a", "e", 1)) 136 | // dag.debugMatrix().let { 137 | // println(it) 138 | // } 139 | // dag.recursive("a", arrayListOf()) 140 | // if (dag.loopbackList.isNotEmpty()) { 141 | // throw RuntimeException("cycle exist:" + dag.loopbackList) 142 | // } 143 | // println(dag.deepPathList) 144 | //} -------------------------------------------------------------------------------- /maat/src/main/java/osp/leobert/android/maat/dag/Edge.kt: -------------------------------------------------------------------------------- 1 | package osp.leobert.android.maat.dag 2 | 3 | /** 4 | *

Package: osp.leobert.android.maat.dag

5 | *

Classname: Edge

6 | * Created by leobert on 2020/9/23. 7 | */ 8 | class Edge(val from: T,val to: T,val weight: Int = 1) { 9 | } -------------------------------------------------------------------------------- /maat/src/main/java/osp/leobert/android/maat/dag/Type.kt: -------------------------------------------------------------------------------- 1 | package osp.leobert.android.maat.dag 2 | 3 | /** 4 | *

Package: osp.leobert.android.maat.dag

5 | *

Classname: Type

6 | * Created by leobert on 2020/9/23. 7 | */ 8 | enum class Type { 9 | X,Y 10 | } -------------------------------------------------------------------------------- /maat/src/main/java/osp/leobert/android/maat/dispatchers.kt: -------------------------------------------------------------------------------- 1 | package osp.leobert.android.maat 2 | 3 | import osp.leobert.android.maat.JobChunk.Companion.info 4 | 5 | /** 6 | *

Package: osp.leobert.android.maat

7 | *

Project: Maat

8 | *

Classname: dispatchers

9 | * Created by leobert on 2020/10/24. 10 | */ 11 | 12 | abstract class Dispatcher { 13 | internal abstract fun start(maat: Maat, currentJobChunk: JobChunk?) 14 | internal abstract fun dispatchOnJobSuccess(maat: Maat, currentJobChunk: JobChunk?, job: JOB) 15 | } 16 | 17 | /** 18 | * 针对Job进行分发 19 | * */ 20 | class JobDispatcher : Dispatcher() { 21 | override fun start(maat: Maat, currentJobChunk: JobChunk?) { 22 | if (currentJobChunk?.haveNext() == true) { 23 | currentJobChunk.next()?.runInit(maat) 24 | } else { 25 | throw MaatException("none jobs!!!") 26 | } 27 | } 28 | 29 | override fun dispatchOnJobSuccess(maat: Maat, currentJobChunk: JobChunk?, job: JOB) { 30 | if (maat.logger.enable) 31 | maat.logger.log("onJobSuccess:${job.uniqueKey}, called on MainThread:${MaatUtil.isMainThread()}") 32 | if (currentJobChunk?.haveNext() == true) { 33 | currentJobChunk.next()?.runInit(maat) 34 | } else { 35 | maat.logger.takeIf { it.enable }?.log("all jobs finished") 36 | maat.callback?.onSuccess?.invoke(maat) 37 | } 38 | } 39 | } 40 | 41 | /** 42 | * 针对Chunk进行分发 43 | * */ 44 | class JobChuckDispatcher : Dispatcher() { 45 | override fun start(maat: Maat, currentJobChunk: JobChunk?) { 46 | if (currentJobChunk?.haveNext() == true) { 47 | while (currentJobChunk.haveNextInChunk()) { 48 | currentJobChunk.next()?.let { 49 | currentJobChunk.markOnHandling(it.uniqueKey) 50 | it.runInit(maat) 51 | } 52 | } 53 | } else { 54 | throw MaatException("none jobs!!!") 55 | } 56 | } 57 | 58 | override fun dispatchOnJobSuccess(maat: Maat, currentJobChunk: JobChunk?, job: JOB) { 59 | if (maat.logger.enable) 60 | maat.logger.log("onJobSuccess:${job.uniqueKey}, called on MainThread:${MaatUtil.isMainThread()}") 61 | 62 | currentJobChunk?.markHandled(job.uniqueKey) 63 | 64 | currentJobChunk?.let { chunk -> 65 | 66 | if (chunk.haveNextInChunk() || chunk.hasHandingJobsInChunk()) { 67 | if (maat.logger.enable) { 68 | maat.logger.log("not finished all:${chunk.info()}") 69 | } 70 | 71 | null 72 | } else { 73 | //fix:https://github.com/leobert-lan/Maat/issues/1 at 2020-11-06 release version 1.1.1; affect 1.1.0 74 | chunk.move2NextChunk() 75 | .let { _ -> //if it (current chunk) is null means all job have bean handled successfully 76 | 77 | // we can believe that if a chunk is not null, it must not be empty. 78 | // And, the local param chunk is the original chunk-header, it can delegate 79 | // the visitation of the chunk current handing. thus, if chunk.haveNext() return true, 80 | // means still existing jobs to execute (in the current chunk), otherwise, all jobs have bean handled. 81 | 82 | if (chunk.haveNext()) { 83 | while (chunk.haveNextInChunk()) { 84 | chunk.next()?.let { 85 | currentJobChunk.markOnHandling(it.uniqueKey) 86 | it.runInit(maat) 87 | } 88 | } 89 | } else { 90 | maat.logger.takeIf { it.enable }?.log("all jobs finished") 91 | maat.callback?.onSuccess?.invoke(maat) 92 | } 93 | } 94 | } 95 | } 96 | } 97 | } -------------------------------------------------------------------------------- /maat/src/test/java/osp/leobert/android/maat/ExampleUnitTest.kt: -------------------------------------------------------------------------------- 1 | package osp.leobert.android.maat 2 | 3 | import org.junit.Test 4 | 5 | import org.junit.Assert.* 6 | 7 | /** 8 | * Example local unit test, which will execute on the development machine (host). 9 | * 10 | * See [testing documentation](http://d.android.com/tools/testing). 11 | */ 12 | class ExampleUnitTest { 13 | @Test 14 | fun addition_isCorrect() { 15 | assertEquals(4, 2 + 2) 16 | } 17 | } -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | include ':maat' 2 | include ':app' 3 | rootProject.name = "Maat" --------------------------------------------------------------------------------