├── .gitignore ├── README.md ├── app ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── com │ │ └── iostyle │ │ └── continuoustrigger │ │ └── ExampleInstrumentedTest.kt │ ├── main │ ├── AndroidManifest.xml │ ├── java │ │ └── com │ │ │ └── iostyle │ │ │ └── continuoustrigger │ │ │ ├── JavaMainActivity.java │ │ │ └── MainActivity.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 │ └── com │ └── iostyle │ └── continuoustrigger │ └── ExampleUnitTest.kt ├── build.gradle ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── settings.gradle └── trigger ├── .gitignore ├── build.gradle ├── consumer-rules.pro ├── proguard-rules.pro └── src ├── androidTest └── java │ └── com │ └── iostyle │ └── trigger │ └── ExampleInstrumentedTest.kt └── main ├── AndroidManifest.xml └── java └── com └── iostyle └── trigger ├── ContinuousTrigger.kt ├── Trigger.kt ├── TriggerDSL.kt └── Util.kt /.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | .gradle 3 | /local.properties 4 | /.idea/ 5 | .DS_Store 6 | /build 7 | /captures 8 | .externalNativeBuild 9 | .cxx 10 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ![](https://github.com/iostyle/ImageRepo/blob/master/icon_continuous_trigger.png) 2 | # ContinuousTrigger [![](https://jitpack.io/v/iostyle/ContinuousTrigger.svg)](https://jitpack.io/#iostyle/ContinuousTrigger) 3 | 用于按序执行一系列任务,可随时绑定(如多接口返回),可对每个步骤设置超时及响应时间。 4 | 5 | 可以将项目clone下来,里面有demo可以修改及测试,喜欢请Star⭐️ 6 | 7 | Step 1. Add it in your root build.gradle at the end of repositories: 8 | 9 | allprojects { 10 | repositories { 11 | ... 12 | maven { url 'https://jitpack.io' } 13 | } 14 | } 15 | Step 2. Add the dependency 16 | 17 | dependencies { 18 | implementation 'com.github.iostyle:ContinuousTrigger:1.0.6' 19 | //自1.0.6版本开始,项目中的依赖方式修改为compileOnly,你需要确保自己的项目中引入了相关的依赖 20 | implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:x.x.x' 21 | } 22 | 23 | ## Attribute & Method 24 | |attribute|description| 25 | |:--:|--| 26 | |id|唯一标识符,用来注册/绑定触发器| 27 | |timeout|超时时间,当前节点超时时间内没有attach将跳转到下一节点,默认值为-1| 28 | |chokeMode|阻塞模式,开启阻塞模式后需要在**你的业务逻辑**处理完成后手动调用next或cancel| 29 | 30 | |method|description| 31 | |:--:|--| 32 | |with|Builder模式构造注册| 33 | |create|Builder模式创建实例并初始化| 34 | |register|实例化方式有序注册| 35 | |attach|根据ID绑定触发器| 36 | |adjustAttach|灵活绑定 (since V1.0.6)| 37 | |next|下一步(阻塞模式下需手动调用),更建议使用cancel传入当前节点ID| 38 | |cancel|根据ID取消对应触发器,如果是当前节点则自动执行下一个| 39 | |~~response~~|~~响应并关闭超时线程~~(V1.0.3版本移除)| 40 | |clear|清空| 41 | |getTriggerInstance|通过主键获取实例| 42 | |saveTriggerInstance|缓存实例,无需手动调用,构造时传入主键即可| 43 | |removeTriggerInstance|从缓存中移除实例,可选是否销毁| 44 | |clearTriggers|清空所有缓存实例| 45 | 46 | ## Version Log 47 | * V 1.0.7 前瞻 48 | - 支持一键生成jar包 49 | - saveTriggerInstance 方法新增返回实例 50 | - next 方法过时,推荐使用cancel结束当前阻塞节点并自动触达下一个节点 51 | - .. 52 | * V 1.0.6 53 | - 支持灵活绑定,无需注册,适用于无顺序要求场景灵活执行 54 | - 替换项目中组件的依赖方式为compileOnly,减少侵入性 55 | 同时注意从这个版本开始,集成方式有所改变,你需要确保自己的项目中引入了相关的依赖 56 | * V 1.0.5 57 | - 支持缓存,通过主键可在任何位置获取实例进行操作 58 | * V 1.0.4 59 | - 支持DSL语法 60 | * V 1.0.3 61 | - 节点级别添加阻塞模式控制 62 | - 移除response方法 降低代码侵入性 63 | 64 | 65 | ## Tip 66 | 下面是Kotlin及Java使用时的示例代码,因为模拟了应用场景所以代码量比较多,核心代码只有注册和绑定仅此而已 67 | 68 | ## Kotlin 69 | ```kotlin 70 | 71 | /** 72 | * 链式调用写法 73 | */ 74 | 75 | trigger = ContinuousTrigger.Builder() 76 | .with( 77 | Trigger().apply { 78 | id = "test1" 79 | timeout = 2000 80 | } 81 | ) 82 | .with( 83 | Trigger().apply { 84 | id = "test2" 85 | // 应用于dialog的阻塞模式 86 | chokeMode = true 87 | } 88 | ) 89 | .with( 90 | Trigger().apply { 91 | id = "test3" 92 | timeout = 2000 93 | } 94 | ) 95 | .create() 96 | 97 | /** 98 | * DSL写法 99 | */ 100 | 101 | val t0 = Trigger().apply { 102 | id = "test1" 103 | timeout = 2000 104 | } 105 | val t1 = Trigger().apply { 106 | id = "test2" 107 | // 应用于dialog的阻塞模式 108 | chokeMode = true 109 | } 110 | val t2 = Trigger().apply { 111 | id = "test3" 112 | timeout = 2000 113 | } 114 | 115 | //name为可选参数 设置name后通过getTriggerInstance(name)获取实例 116 | trigger = (ContinuousTrigger.Builder("myTrigger") with t0 with t1 with t2).create() 117 | 118 | GlobalScope.launch { 119 | delay(1500) 120 | withContext(Dispatchers.Main) { 121 | trigger?.attach("test1", object : Trigger.Strike { 122 | override fun strike() { 123 | Log.e("trigger", "test1") 124 | } 125 | }) 126 | } 127 | } 128 | 129 | //在任何位置可以根据名字获取实例 130 | getTriggerInstance("myTrigger")?.attach("test2", object : Trigger.Strike { 131 | override fun strike() { 132 | Log.e("trigger", "test2") 133 | AlertDialog.Builder(this@MainActivity).setMessage("test2") 134 | .setOnDismissListener { 135 | trigger?.next() 136 | }.show() 137 | } 138 | }) 139 | 140 | GlobalScope.launch { 141 | delay(6000) 142 | withContext(Dispatchers.Main) { 143 | trigger?.attach("test3", object : Trigger.Strike { 144 | override fun strike() { 145 | Log.e("trigger", "test3") 146 | } 147 | }) 148 | } 149 | } 150 | 151 | /** 152 | * 灵活绑定写法 153 | */ 154 | saveTriggerInstance("adjustTrigger") 155 | 156 | getTriggerInstance("adjustTrigger")?.run { 157 | adjustAttach("t1", object : Trigger.Strike { 158 | override fun strike() { 159 | Log.e("trigger", "t1") 160 | AlertDialog.Builder(this@MainActivity).setMessage("t1") 161 | .setOnDismissListener { 162 | next() 163 | }.show() 164 | } 165 | }, true) 166 | } 167 | 168 | getTriggerInstance("adjustTrigger")?.run { 169 | adjustAttach( 170 | "t2", object : Trigger.Strike { 171 | override fun strike() { 172 | Log.e("trigger", "t2") 173 | GlobalScope.launch { 174 | delay(1000) 175 | next() 176 | } 177 | } 178 | }, true 179 | ) 180 | } 181 | 182 | getTriggerInstance("adjustTrigger")?.adjustAttach( 183 | "t3", 184 | object : Trigger.Strike { 185 | override fun strike() { 186 | Log.e("trigger", "t3") 187 | } 188 | }, 189 | ) 190 | ``` 191 | 192 | ## Java 193 | ```java 194 | Trigger t0 = new Trigger(); 195 | t0.setId("test1"); 196 | t0.setTimeout(2000); 197 | 198 | Trigger t1 = new Trigger(); 199 | t1.setId("test2"); 200 | t1.setChokeMode(true); 201 | 202 | Trigger t2 = new Trigger(); 203 | t2.setId("test3"); 204 | t2.setTimeout(2000); 205 | 206 | //name为可选参数 用于在任意位置获取trigger实例 207 | trigger = new ContinuousTrigger.Builder("myTrigger") 208 | .with(t0) 209 | .with(t1) 210 | .with(t2) 211 | .create(); 212 | 213 | new Thread(new Runnable() { 214 | @Override 215 | public void run() { 216 | try { 217 | Thread.sleep(1500); 218 | } catch (InterruptedException e) { 219 | e.printStackTrace(); 220 | } 221 | runOnUiThread(new Runnable() { 222 | @Override 223 | public void run() { 224 | if (trigger != null) 225 | trigger.attach("test1", new Trigger.Strike() { 226 | @Override 227 | public void strike() { 228 | Log.e("trigger", "test1"); 229 | } 230 | }); 231 | } 232 | }); 233 | 234 | } 235 | }).start(); 236 | 237 | new Thread(new Runnable() { 238 | @Override 239 | public void run() { 240 | runOnUiThread(new Runnable() { 241 | @Override 242 | public void run() { 243 | UtilKt.getTriggerInstance("myTrigger").attach("test2", new Trigger.Strike() { 244 | @Override 245 | public void strike() { 246 | Log.e("trigger", "test2"); 247 | new AlertDialog.Builder(JavaMainActivity.this).setMessage("test2") 248 | .setOnDismissListener(new DialogInterface.OnDismissListener() { 249 | @Override 250 | public void onDismiss(DialogInterface dialog) { 251 | if (trigger != null) 252 | trigger.next(); 253 | } 254 | }).show(); 255 | } 256 | }); 257 | } 258 | }); 259 | 260 | } 261 | }).start(); 262 | 263 | new Thread(new Runnable() { 264 | @Override 265 | public void run() { 266 | try { 267 | Thread.sleep(6000); 268 | } catch (InterruptedException e) { 269 | e.printStackTrace(); 270 | } 271 | runOnUiThread(new Runnable() { 272 | @Override 273 | public void run() { 274 | if (trigger != null) 275 | trigger.attach("test3", new Trigger.Strike() { 276 | @Override 277 | public void strike() { 278 | Log.e("trigger", "test3"); 279 | UtilKt.removeTriggerInstance("myTrigger"); 280 | } 281 | }); 282 | } 283 | }); 284 | } 285 | }).start(); 286 | ``` 287 | ## 一起干杯 🍺~( ̄▽ ̄)~* 288 |
289 | 290 | 291 |
292 | -------------------------------------------------------------------------------- /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | apply plugin: 'kotlin-android' 3 | apply plugin: 'kotlin-android-extensions' 4 | 5 | 6 | android { 7 | compileSdkVersion 29 8 | 9 | defaultConfig { 10 | applicationId "com.iostyle.continuoustrigger" 11 | minSdkVersion 14 12 | targetSdkVersion 29 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 | 28 | dependencies { 29 | implementation project(path: ':trigger') 30 | implementation fileTree(dir: 'libs', include: ['*.jar']) 31 | 32 | testImplementation 'junit:junit:4.12' 33 | androidTestImplementation 'androidx.test.ext:junit:1.1.1' 34 | androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0' 35 | 36 | implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.3.0-RC2' 37 | implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" 38 | implementation 'androidx.appcompat:appcompat:1.1.0' 39 | implementation 'androidx.core:core-ktx:1.2.0' 40 | implementation 'androidx.constraintlayout:constraintlayout:1.1.3' 41 | } 42 | -------------------------------------------------------------------------------- /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/androidTest/java/com/iostyle/continuoustrigger/ExampleInstrumentedTest.kt: -------------------------------------------------------------------------------- 1 | package com.iostyle.continuoustrigger 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("com.iostyle.continuoustrigger", appContext.packageName) 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | -------------------------------------------------------------------------------- /app/src/main/java/com/iostyle/continuoustrigger/JavaMainActivity.java: -------------------------------------------------------------------------------- 1 | package com.iostyle.continuoustrigger; 2 | 3 | import android.content.DialogInterface; 4 | import android.os.Bundle; 5 | import android.util.Log; 6 | 7 | import androidx.annotation.Nullable; 8 | import androidx.appcompat.app.AlertDialog; 9 | import androidx.appcompat.app.AppCompatActivity; 10 | 11 | import com.iostyle.trigger.ContinuousTrigger; 12 | import com.iostyle.trigger.Trigger; 13 | import com.iostyle.trigger.UtilKt; 14 | 15 | public class JavaMainActivity extends AppCompatActivity { 16 | 17 | private ContinuousTrigger trigger; 18 | 19 | @Override 20 | protected void onCreate(@Nullable Bundle savedInstanceState) { 21 | super.onCreate(savedInstanceState); 22 | setContentView(R.layout.activity_main); 23 | init(); 24 | } 25 | 26 | private void init() { 27 | Trigger t0 = new Trigger(); 28 | t0.setId("test1"); 29 | t0.setTimeout(2000); 30 | 31 | Trigger t1 = new Trigger(); 32 | t1.setId("test2"); 33 | t1.setChokeMode(true); 34 | 35 | Trigger t2 = new Trigger(); 36 | t2.setId("test3"); 37 | t2.setTimeout(2000); 38 | 39 | //name为可选参数 用于在任意位置获取trigger实例 40 | trigger = new ContinuousTrigger.Builder("myTrigger") 41 | .with(t0) 42 | .with(t1) 43 | .with(t2) 44 | .create(); 45 | 46 | new Thread(new Runnable() { 47 | @Override 48 | public void run() { 49 | try { 50 | Thread.sleep(1500); 51 | } catch (InterruptedException e) { 52 | e.printStackTrace(); 53 | } 54 | runOnUiThread(new Runnable() { 55 | @Override 56 | public void run() { 57 | if (trigger != null) 58 | trigger.attach("test1", new Trigger.Strike() { 59 | @Override 60 | public void strike() { 61 | Log.e("trigger", "test1"); 62 | } 63 | }); 64 | } 65 | }); 66 | 67 | } 68 | }).start(); 69 | 70 | new Thread(new Runnable() { 71 | @Override 72 | public void run() { 73 | runOnUiThread(new Runnable() { 74 | @Override 75 | public void run() { 76 | UtilKt.getTriggerInstance("myTrigger").attach("test2", new Trigger.Strike() { 77 | @Override 78 | public void strike() { 79 | Log.e("trigger", "test2"); 80 | new AlertDialog.Builder(JavaMainActivity.this).setMessage("test2") 81 | .setOnDismissListener(new DialogInterface.OnDismissListener() { 82 | @Override 83 | public void onDismiss(DialogInterface dialog) { 84 | if (trigger != null) 85 | trigger.next(); 86 | } 87 | }).show(); 88 | } 89 | }); 90 | } 91 | }); 92 | 93 | } 94 | }).start(); 95 | 96 | new Thread(new Runnable() { 97 | @Override 98 | public void run() { 99 | try { 100 | Thread.sleep(6000); 101 | } catch (InterruptedException e) { 102 | e.printStackTrace(); 103 | } 104 | runOnUiThread(new Runnable() { 105 | @Override 106 | public void run() { 107 | if (trigger != null) 108 | trigger.attach("test3", new Trigger.Strike() { 109 | @Override 110 | public void strike() { 111 | Log.e("trigger", "test3"); 112 | UtilKt.removeTriggerInstance("myTrigger"); 113 | } 114 | }); 115 | } 116 | }); 117 | } 118 | }).start(); 119 | } 120 | } 121 | -------------------------------------------------------------------------------- /app/src/main/java/com/iostyle/continuoustrigger/MainActivity.kt: -------------------------------------------------------------------------------- 1 | package com.iostyle.continuoustrigger 2 | 3 | import androidx.appcompat.app.AppCompatActivity 4 | import android.os.Bundle 5 | import android.util.Log 6 | import androidx.appcompat.app.AlertDialog 7 | import com.iostyle.trigger.* 8 | import kotlinx.coroutines.* 9 | 10 | class MainActivity : AppCompatActivity() { 11 | 12 | private var trigger: ContinuousTrigger? = null 13 | 14 | override fun onCreate(savedInstanceState: Bundle?) { 15 | super.onCreate(savedInstanceState) 16 | setContentView(R.layout.activity_main) 17 | init() 18 | } 19 | 20 | private fun init() { 21 | /** 22 | * 链式调用写法 23 | */ 24 | // trigger = ContinuousTrigger.Builder() 25 | // .with( 26 | // Trigger().apply { 27 | // id = "test1" 28 | // timeout = 2000 29 | // } 30 | // ) 31 | // .with( 32 | // Trigger().apply { 33 | // id = "test2" 34 | // // 应用于dialog的阻塞模式 35 | // chokeMode = true 36 | // } 37 | // ) 38 | // .with( 39 | // Trigger().apply { 40 | // id = "test3" 41 | // timeout = 2000 42 | // } 43 | // ) 44 | // .create() 45 | 46 | /** 47 | * DSL写法 48 | */ 49 | // val t0 = Trigger().apply { 50 | // id = "test1" 51 | // timeout = 2000 52 | // } 53 | // val t1 = Trigger().apply { 54 | // id = "test2" 55 | // // 应用于dialog的阻塞模式 56 | // chokeMode = true 57 | // } 58 | // val t2 = Trigger().apply { 59 | // id = "test3" 60 | // timeout = 2000 61 | // } 62 | // //name为可选参数 设置name后通过getTriggerInstance(name)获取实例 63 | // trigger = (ContinuousTrigger.Builder("myTrigger") with t0 with t1 with t2).create() 64 | // 65 | // GlobalScope.launch { 66 | // delay(1500) 67 | // withContext(Dispatchers.Main) { 68 | // trigger?.attach("test1", object : Trigger.Strike { 69 | // override fun strike() { 70 | // Log.e("trigger", "test1") 71 | // } 72 | // }) 73 | // } 74 | // } 75 | // 76 | // //在任何位置可以根据名字获取实例 77 | // getTriggerInstance("myTrigger")?.attach("test2", object : Trigger.Strike { 78 | // override fun strike() { 79 | // Log.e("trigger", "test2") 80 | // AlertDialog.Builder(this@MainActivity).setMessage("test2") 81 | // .setOnDismissListener { 82 | // trigger?.next() 83 | // }.show() 84 | // } 85 | // }) 86 | // 87 | // GlobalScope.launch { 88 | // delay(6000) 89 | // withContext(Dispatchers.Main) { 90 | // trigger?.attach("test3", object : Trigger.Strike { 91 | // override fun strike() { 92 | // Log.e("trigger", "test3") 93 | // removeTriggerInstance("myTrigger") 94 | // } 95 | // }) 96 | // } 97 | // } 98 | 99 | /** 100 | * 灵活绑定写法 101 | */ 102 | saveTriggerInstance("adjustTrigger") 103 | 104 | getTriggerInstance("adjustTrigger")?.run { 105 | adjustAttach("t1", object : Trigger.Strike { 106 | override fun strike() { 107 | Log.e("trigger", "t1") 108 | AlertDialog.Builder(this@MainActivity).setMessage("t1") 109 | .setOnDismissListener { 110 | next() 111 | }.show() 112 | } 113 | }, true) 114 | } 115 | 116 | getTriggerInstance("adjustTrigger")?.run { 117 | adjustAttach( 118 | "t2", object : Trigger.Strike { 119 | override fun strike() { 120 | Log.e("trigger", "t2") 121 | GlobalScope.launch { 122 | delay(1000) 123 | next() 124 | } 125 | } 126 | }, true 127 | ) 128 | } 129 | 130 | getTriggerInstance("adjustTrigger")?.adjustAttach( 131 | "t3", 132 | object : Trigger.Strike { 133 | override fun strike() { 134 | Log.e("trigger", "t3") 135 | } 136 | }, 137 | ) 138 | 139 | } 140 | 141 | override fun onDestroy() { 142 | super.onDestroy() 143 | trigger?.clear() 144 | clearTriggers() 145 | } 146 | } 147 | -------------------------------------------------------------------------------- /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/iostyle/ContinuousTrigger/19890df8f71d1144eaca524276602873f5f6cb47/app/src/main/res/mipmap-hdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-hdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/iostyle/ContinuousTrigger/19890df8f71d1144eaca524276602873f5f6cb47/app/src/main/res/mipmap-hdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-mdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/iostyle/ContinuousTrigger/19890df8f71d1144eaca524276602873f5f6cb47/app/src/main/res/mipmap-mdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-mdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/iostyle/ContinuousTrigger/19890df8f71d1144eaca524276602873f5f6cb47/app/src/main/res/mipmap-mdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/iostyle/ContinuousTrigger/19890df8f71d1144eaca524276602873f5f6cb47/app/src/main/res/mipmap-xhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/iostyle/ContinuousTrigger/19890df8f71d1144eaca524276602873f5f6cb47/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/iostyle/ContinuousTrigger/19890df8f71d1144eaca524276602873f5f6cb47/app/src/main/res/mipmap-xxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/iostyle/ContinuousTrigger/19890df8f71d1144eaca524276602873f5f6cb47/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/iostyle/ContinuousTrigger/19890df8f71d1144eaca524276602873f5f6cb47/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/iostyle/ContinuousTrigger/19890df8f71d1144eaca524276602873f5f6cb47/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 | 7 | -------------------------------------------------------------------------------- /app/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | ContinuousTrigger 3 | 4 | -------------------------------------------------------------------------------- /app/src/main/res/values/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /app/src/test/java/com/iostyle/continuoustrigger/ExampleUnitTest.kt: -------------------------------------------------------------------------------- 1 | package com.iostyle.continuoustrigger 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 | } 18 | -------------------------------------------------------------------------------- /build.gradle: -------------------------------------------------------------------------------- 1 | // Top-level build file where you can add configuration options common to all sub-projects/modules. 2 | 3 | buildscript { 4 | ext.kotlin_version = '1.4.31' 5 | repositories { 6 | google() 7 | jcenter() 8 | 9 | } 10 | dependencies { 11 | classpath 'com.android.tools.build:gradle:4.1.1' 12 | classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" 13 | classpath 'com.github.dcendents:android-maven-gradle-plugin:2.0' 14 | 15 | // NOTE: Do not place your application dependencies here; they belong 16 | // in the individual module build.gradle files 17 | } 18 | } 19 | 20 | allprojects { 21 | repositories { 22 | google() 23 | jcenter() 24 | 25 | } 26 | } 27 | 28 | task clean(type: Delete) { 29 | delete rootProject.buildDir 30 | } 31 | -------------------------------------------------------------------------------- /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=-Xmx1536m 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 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/iostyle/ContinuousTrigger/19890df8f71d1144eaca524276602873f5f6cb47/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Wed Dec 09 10:21:14 CST 2020 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-6.7-all.zip 7 | -------------------------------------------------------------------------------- /gradlew: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env sh 2 | 3 | ############################################################################## 4 | ## 5 | ## Gradle start up script for UN*X 6 | ## 7 | ############################################################################## 8 | 9 | # Attempt to set APP_HOME 10 | # Resolve links: $0 may be a link 11 | PRG="$0" 12 | # Need this for relative symlinks. 13 | while [ -h "$PRG" ] ; do 14 | ls=`ls -ld "$PRG"` 15 | link=`expr "$ls" : '.*-> \(.*\)$'` 16 | if expr "$link" : '/.*' > /dev/null; then 17 | PRG="$link" 18 | else 19 | PRG=`dirname "$PRG"`"/$link" 20 | fi 21 | done 22 | SAVED="`pwd`" 23 | cd "`dirname \"$PRG\"`/" >/dev/null 24 | APP_HOME="`pwd -P`" 25 | cd "$SAVED" >/dev/null 26 | 27 | APP_NAME="Gradle" 28 | APP_BASE_NAME=`basename "$0"` 29 | 30 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 31 | DEFAULT_JVM_OPTS="" 32 | 33 | # Use the maximum available, or set MAX_FD != -1 to use that value. 34 | MAX_FD="maximum" 35 | 36 | warn () { 37 | echo "$*" 38 | } 39 | 40 | die () { 41 | echo 42 | echo "$*" 43 | echo 44 | exit 1 45 | } 46 | 47 | # OS specific support (must be 'true' or 'false'). 48 | cygwin=false 49 | msys=false 50 | darwin=false 51 | nonstop=false 52 | case "`uname`" in 53 | CYGWIN* ) 54 | cygwin=true 55 | ;; 56 | Darwin* ) 57 | darwin=true 58 | ;; 59 | MINGW* ) 60 | msys=true 61 | ;; 62 | NONSTOP* ) 63 | nonstop=true 64 | ;; 65 | esac 66 | 67 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 68 | 69 | # Determine the Java command to use to start the JVM. 70 | if [ -n "$JAVA_HOME" ] ; then 71 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 72 | # IBM's JDK on AIX uses strange locations for the executables 73 | JAVACMD="$JAVA_HOME/jre/sh/java" 74 | else 75 | JAVACMD="$JAVA_HOME/bin/java" 76 | fi 77 | if [ ! -x "$JAVACMD" ] ; then 78 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 79 | 80 | Please set the JAVA_HOME variable in your environment to match the 81 | location of your Java installation." 82 | fi 83 | else 84 | JAVACMD="java" 85 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 86 | 87 | Please set the JAVA_HOME variable in your environment to match the 88 | location of your Java installation." 89 | fi 90 | 91 | # Increase the maximum file descriptors if we can. 92 | if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then 93 | MAX_FD_LIMIT=`ulimit -H -n` 94 | if [ $? -eq 0 ] ; then 95 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then 96 | MAX_FD="$MAX_FD_LIMIT" 97 | fi 98 | ulimit -n $MAX_FD 99 | if [ $? -ne 0 ] ; then 100 | warn "Could not set maximum file descriptor limit: $MAX_FD" 101 | fi 102 | else 103 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" 104 | fi 105 | fi 106 | 107 | # For Darwin, add options to specify how the application appears in the dock 108 | if $darwin; then 109 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" 110 | fi 111 | 112 | # For Cygwin, switch paths to Windows format before running java 113 | if $cygwin ; then 114 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` 115 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` 116 | JAVACMD=`cygpath --unix "$JAVACMD"` 117 | 118 | # We build the pattern for arguments to be converted via cygpath 119 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` 120 | SEP="" 121 | for dir in $ROOTDIRSRAW ; do 122 | ROOTDIRS="$ROOTDIRS$SEP$dir" 123 | SEP="|" 124 | done 125 | OURCYGPATTERN="(^($ROOTDIRS))" 126 | # Add a user-defined pattern to the cygpath arguments 127 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then 128 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" 129 | fi 130 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 131 | i=0 132 | for arg in "$@" ; do 133 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` 134 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option 135 | 136 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition 137 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` 138 | else 139 | eval `echo args$i`="\"$arg\"" 140 | fi 141 | i=$((i+1)) 142 | done 143 | case $i in 144 | (0) set -- ;; 145 | (1) set -- "$args0" ;; 146 | (2) set -- "$args0" "$args1" ;; 147 | (3) set -- "$args0" "$args1" "$args2" ;; 148 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;; 149 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; 150 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; 151 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; 152 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; 153 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; 154 | esac 155 | fi 156 | 157 | # Escape application args 158 | save () { 159 | for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done 160 | echo " " 161 | } 162 | APP_ARGS=$(save "$@") 163 | 164 | # Collect all arguments for the java command, following the shell quoting and substitution rules 165 | eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS" 166 | 167 | # by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong 168 | if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then 169 | cd "$(dirname "$0")" 170 | fi 171 | 172 | exec "$JAVACMD" "$@" 173 | -------------------------------------------------------------------------------- /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 | set DIRNAME=%~dp0 12 | if "%DIRNAME%" == "" set DIRNAME=. 13 | set APP_BASE_NAME=%~n0 14 | set APP_HOME=%DIRNAME% 15 | 16 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 17 | set DEFAULT_JVM_OPTS= 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 Windows variants 50 | 51 | if not "%OS%" == "Windows_NT" goto win9xME_args 52 | 53 | :win9xME_args 54 | @rem Slurp the command line arguments. 55 | set CMD_LINE_ARGS= 56 | set _SKIP=2 57 | 58 | :win9xME_args_slurp 59 | if "x%~1" == "x" goto execute 60 | 61 | set CMD_LINE_ARGS=%* 62 | 63 | :execute 64 | @rem Setup the command line 65 | 66 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 67 | 68 | @rem Execute Gradle 69 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 70 | 71 | :end 72 | @rem End local scope for the variables with windows NT shell 73 | if "%ERRORLEVEL%"=="0" goto mainEnd 74 | 75 | :fail 76 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 77 | rem the _cmd.exe /c_ return code! 78 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 79 | exit /b 1 80 | 81 | :mainEnd 82 | if "%OS%"=="Windows_NT" endlocal 83 | 84 | :omega 85 | -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | rootProject.name='ContinuousTrigger' 2 | include ':app' 3 | include ':trigger' 4 | -------------------------------------------------------------------------------- /trigger/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /trigger/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.library' 2 | apply plugin: 'kotlin-android' 3 | //apply plugin: 'kotlin-android-extensions' 4 | apply plugin: 'com.github.dcendents.android-maven' 5 | 6 | group='com.github.iostyle' 7 | 8 | android { 9 | compileSdkVersion 29 10 | 11 | defaultConfig { 12 | versionCode 6 13 | versionName "1.0.6" 14 | 15 | testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" 16 | consumerProguardFiles 'consumer-rules.pro' 17 | } 18 | 19 | buildTypes { 20 | release { 21 | minifyEnabled false 22 | proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' 23 | } 24 | } 25 | 26 | } 27 | 28 | dependencies { 29 | implementation fileTree(dir: 'libs', include: ['*.jar']) 30 | // compileOnly "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" 31 | // compileOnly 'androidx.appcompat:appcompat:1.2.0' 32 | // compileOnly 'androidx.core:core-ktx:1.3.2' 33 | compileOnly 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.3.9' 34 | } 35 | 36 | // 生成jar包的配置如下: 37 | def JAR_PATH = "build/intermediates/runtime_library_classes_jar/release/" // 待打包文件的位置 38 | def JAR_NAME = "classes.jar" // 待打包文件的名字 39 | def DESTINATION_PATH = "libs" // 生成jar包的位置 40 | def NEW_NAME = "libtrigger.jar" // 生成jar包的名字 41 | 42 | task makeJar(type: Copy) { 43 | delete DESTINATION_PATH + NEW_NAME 44 | from(JAR_PATH + JAR_NAME) 45 | into(DESTINATION_PATH) 46 | rename(JAR_NAME, NEW_NAME) 47 | } 48 | 49 | makeJar.dependsOn(build) -------------------------------------------------------------------------------- /trigger/consumer-rules.pro: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/iostyle/ContinuousTrigger/19890df8f71d1144eaca524276602873f5f6cb47/trigger/consumer-rules.pro -------------------------------------------------------------------------------- /trigger/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 | -------------------------------------------------------------------------------- /trigger/src/androidTest/java/com/iostyle/trigger/ExampleInstrumentedTest.kt: -------------------------------------------------------------------------------- 1 | package com.iostyle.trigger 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("com.iostyle.trigger.test", appContext.packageName) 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /trigger/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 3 | -------------------------------------------------------------------------------- /trigger/src/main/java/com/iostyle/trigger/ContinuousTrigger.kt: -------------------------------------------------------------------------------- 1 | package com.iostyle.trigger 2 | 3 | import android.util.Log 4 | import kotlinx.coroutines.* 5 | import java.util.concurrent.ConcurrentLinkedQueue 6 | 7 | /** 8 | * 多线程连续触发器 9 | */ 10 | class ContinuousTrigger { 11 | 12 | private var triggerList: ConcurrentLinkedQueue? = null 13 | private var currentJob: Job? = null 14 | 15 | private var blockNode: Trigger? = null 16 | private var debugMode = false 17 | 18 | constructor() 19 | 20 | constructor(triggerList: ConcurrentLinkedQueue?) { 21 | this.triggerList = triggerList 22 | } 23 | 24 | 25 | class Builder { 26 | private var name: String? = null 27 | private var triggerList = ConcurrentLinkedQueue() 28 | 29 | constructor() 30 | constructor(name: String?) { 31 | this.name = name 32 | } 33 | 34 | @TriggerDSL 35 | infix fun with(trigger: Trigger): Builder { 36 | triggerList.offer(trigger) 37 | return this 38 | } 39 | 40 | fun create(): ContinuousTrigger { 41 | return ContinuousTrigger(triggerList).also { 42 | it.next() 43 | if (!name.isNullOrBlank()) saveTriggerInstance(name!!, it) 44 | } 45 | } 46 | } 47 | 48 | //按序注册 49 | @TriggerDSL 50 | infix fun register(trigger: Trigger): ContinuousTrigger { 51 | if (debugMode) log("ContinuousTrigger register ${triggerList?.size}: ${trigger.id}") 52 | if (triggerList == null) triggerList = ConcurrentLinkedQueue() 53 | triggerList?.offer(trigger) 54 | if (blockNode == null) { 55 | if (debugMode) log("ContinuousTrigger autoStart") 56 | next() 57 | } 58 | return this 59 | } 60 | 61 | //绑定执行 为Java代码提供的重载 62 | @Synchronized 63 | fun attach(id: String, strike: Trigger.Strike) { 64 | attach(id, strike, false) 65 | } 66 | 67 | //绑定执行 68 | @Synchronized 69 | fun attach(id: String, strike: Trigger.Strike, adjustAttach: Boolean = false) { 70 | if (debugMode) log("ContinuousTrigger attach ${id}") 71 | 72 | //阻塞唤醒 73 | if (tryWakeUp(id, strike)) { 74 | if (debugMode) log("ContinuousTrigger blockNode wakeup") 75 | } else if (triggerList?.isEmpty() == false) { 76 | triggerList?.forEach { 77 | if (it.id == id) { 78 | it.strike = strike 79 | return 80 | } 81 | } 82 | /** 83 | * 代码走到这里代表你attach的事件没有被绑定或者已经超时被跳过 84 | */ 85 | if (adjustAttach) 86 | adjustAttach(id, strike) 87 | } else { 88 | /** 89 | * 代码走到这里意味着所有的事件都已经超时 90 | */ 91 | if (adjustAttach) 92 | adjustAttach(id, strike) 93 | } 94 | } 95 | 96 | /** 97 | * 灵活绑定 内部调用用于已超时的补充或者id未注册场景 98 | * 外部使用的话如果不需要顺序控制则不需要注册 直接调用adjustAttach即可按调用顺序执行 99 | */ 100 | @Synchronized 101 | fun adjustAttach(id: String, strike: Trigger.Strike, chokeMode: Boolean = false) { 102 | register(Trigger().also { 103 | it.id = id 104 | it.chokeMode = chokeMode 105 | it.strike = strike 106 | }) 107 | } 108 | 109 | private fun tryWakeUp(id: String, strike: Trigger.Strike): Boolean { 110 | if (isCurrentNode(id)) { 111 | currentJob?.cancel() 112 | strike.strike() 113 | if (blockNode?.chokeMode == false) next() 114 | return true 115 | } 116 | return false 117 | } 118 | 119 | private fun isCurrentNode(id: String): Boolean { 120 | return blockNode != null && blockNode!!.id == id 121 | } 122 | 123 | //下一步 124 | @Synchronized 125 | fun next() { 126 | currentJob?.cancel() 127 | blockNode = triggerList?.poll() 128 | if (debugMode) log("ContinuousTrigger next ${blockNode?.id}") 129 | blockNode?.run { 130 | if (invalid) { 131 | if (debugMode) log("ContinuousTrigger invalid $id") 132 | next() 133 | return 134 | } 135 | strike?.run { 136 | strike() 137 | if (!chokeMode) next() 138 | } ?: ( 139 | if (timeout > 0) 140 | currentJob = GlobalScope.launch { 141 | delay(timeout) 142 | if (debugMode) log("ContinuousTrigger timeout $id") 143 | withContext(Dispatchers.Main) { 144 | next() 145 | } 146 | } 147 | ) 148 | } ?: clear() 149 | } 150 | 151 | fun cancel(id: String) { 152 | if (debugMode) log("ContinuousTrigger cancel $id") 153 | if (triggerList?.isEmpty() == false) 154 | kotlin.run looper@{ 155 | triggerList?.forEach { 156 | if (it.id == id) { 157 | it.invalid = true 158 | return@looper 159 | } 160 | } 161 | } 162 | if (isCurrentNode(id)) { 163 | if (debugMode) log("ContinuousTrigger cancel next $id") 164 | next() 165 | } 166 | } 167 | 168 | //清空 169 | fun clear() { 170 | if (debugMode) log("ContinuousTrigger clear") 171 | blockNode = null 172 | currentJob?.cancel() 173 | triggerList?.clear() 174 | } 175 | 176 | fun log(content: String?) { 177 | content?.let { 178 | Log.e("Trigger", it) 179 | } 180 | } 181 | } -------------------------------------------------------------------------------- /trigger/src/main/java/com/iostyle/trigger/Trigger.kt: -------------------------------------------------------------------------------- 1 | package com.iostyle.trigger 2 | 3 | class Trigger { 4 | var id: String? = null 5 | 6 | //超时时间 -1为持续等待 7 | var timeout: Long = -1L 8 | 9 | //已失效 10 | var invalid = false 11 | 12 | var strike: Strike? = null 13 | 14 | //阻塞模式 15 | var chokeMode = false 16 | 17 | interface Strike { 18 | fun strike() 19 | } 20 | } -------------------------------------------------------------------------------- /trigger/src/main/java/com/iostyle/trigger/TriggerDSL.kt: -------------------------------------------------------------------------------- 1 | package com.iostyle.trigger 2 | 3 | @DslMarker 4 | annotation class TriggerDSL { 5 | } -------------------------------------------------------------------------------- /trigger/src/main/java/com/iostyle/trigger/Util.kt: -------------------------------------------------------------------------------- 1 | package com.iostyle.trigger 2 | 3 | import java.util.concurrent.ConcurrentHashMap 4 | 5 | private var continuousTriggers: ConcurrentHashMap? = null 6 | 7 | fun getTriggerInstance(name: String): ContinuousTrigger? { 8 | return continuousTriggers?.get(name) 9 | } 10 | 11 | fun saveTriggerInstance( 12 | name: String, 13 | continuousTrigger: ContinuousTrigger? = null 14 | ): ContinuousTrigger { 15 | if (continuousTriggers == null) { 16 | continuousTriggers = ConcurrentHashMap() 17 | } 18 | val trigger = continuousTrigger ?: ContinuousTrigger() 19 | continuousTriggers?.put(name, trigger) 20 | return trigger 21 | } 22 | 23 | fun removeTriggerInstance(name: String): ContinuousTrigger? { 24 | return removeTriggerInstance(name, true) 25 | } 26 | 27 | fun removeTriggerInstance(name: String, clear: Boolean): ContinuousTrigger? { 28 | return continuousTriggers?.remove(name)?.apply { if (clear) clear() } 29 | } 30 | 31 | fun clearTriggers() { 32 | continuousTriggers?.forEach { it.value.clear() } 33 | continuousTriggers?.clear() 34 | continuousTriggers = null 35 | } --------------------------------------------------------------------------------