Skip to content

Commit b222afe

Browse files
author
Ven
committed
init
init init init init init init init init
1 parent c4dc7f7 commit b222afe

File tree

9 files changed

+146
-29
lines changed

9 files changed

+146
-29
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,5 @@ proguard/
2727
*.iml
2828
.gradle
2929
.idea
30-
build
30+
build
31+
readme1.md

README.md

Lines changed: 113 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,115 @@
11

2-
<style>
3-
.center {
4-
text-align: center;
2+
3+
# Assists
4+
Android无障碍服务(AccessibilityService)开发框架,快速开发复杂自动化任务、远程协助、监听等
5+
***
6+
7+
## Android无障碍服务能做什么
8+
利用Android无障碍服务可以开发一些Android系统内的自动化任务,比如经典的微信自动抢红包、支付宝蚂蚁森林自动浇水、芭芭农场自动施肥等
9+
10+
还可以开发远程协助功能,市面上向日葵等一些远程协助功能就是利用无障碍服务和投屏权限开发的
11+
12+
还能开发一些拓客、引流、营销系统,抖音自动点赞评论、微博自动转发评论关注等
13+
14+
总之,利用Android的无障碍服务可以开发各种自动化的任务或者界面信息监听、远程协助等
15+
16+
## Assists开发框架能做什么
17+
18+
按照Google官方文档继承实现的无障碍服务,对于复杂的自动化任务,不仅代码逻辑实现不清晰,后期的修改维护也会很头疼,所以在实践过程中实现了这个框架
19+
20+
在这个框架下开发Android无障碍服务业务可以让你的业务开发更加快速、逻辑更加健壮且容易维护。
21+
22+
## 快速开始
23+
### 添加依赖
24+
1. 将JitPack仓库添加到根目录build.gradle文件中
25+
26+
```groovy
27+
allprojects {
28+
repositories {
29+
//添加JitPack仓库
30+
maven { url 'https://jitpack.io' }
31+
}
532
}
6-
</style>
7-
<h1 class="center">Assists</h1>
8-
<h5 class="center">快速开发且易维护的Android无障碍业务框架</h5>
33+
```
34+
35+
2. 添加依赖到主模块的build.gradle中
36+
```groovy
37+
dependencies {
38+
//添加依赖
39+
implementation 'com.github.ven-coder:assists:1.0.0'
40+
}
41+
```
42+
### 主模块AndroidManifest.xml中注册服务
43+
44+
```xml
45+
<?xml version="1.0" encoding="utf-8"?>
46+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
47+
xmlns:tools="http://schemas.android.com/tools"
48+
package="com.ven.assists.simple">
49+
50+
<application
51+
android:name="com.ven.assists.simple.App"
52+
android:allowBackup="true"
53+
android:icon="@mipmap/ic_launcher"
54+
android:label="@string/app_name"
55+
android:requestLegacyExternalStorage="true"
56+
android:roundIcon="@mipmap/ic_launcher_round"
57+
android:supportsRtl="true"
58+
android:theme="@style/AppTheme"
59+
android:usesCleartextTraffic="true">
60+
<!-- 添加以下代码 -->
61+
<service
62+
android:name="com.ven.assist.AssistsService"
63+
android:enabled="true"
64+
android:exported="true"
65+
android:permission="android.permission.BIND_ACCESSIBILITY_SERVICE">
66+
<intent-filter>
67+
<action android:name="android.accessibilityservice.AccessibilityService" />
68+
</intent-filter>
69+
<meta-data
70+
android:name="android.accessibilityservice"
71+
android:resource="@xml/assists_service" />
72+
</service>
73+
</application>
74+
75+
</manifest>
76+
```
77+
### 实现业务逻辑
78+
1. 继承```StepImpl```实现`onImpl(collector: StepCollector)`接口,通过```collector.next()```实现步骤逻辑
79+
80+
```kotlin
81+
class OpenWechat:StepImpl {
82+
override fun onImpl(collector: StepCollector) {
83+
collector.next(1) {
84+
//步骤1逻辑
85+
//打开微信
86+
Intent().apply {
87+
addCategory(Intent.CATEGORY_LAUNCHER)
88+
addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
89+
component = ComponentName("com.tencent.mm", "com.tencent.mm.ui.LauncherUI")
90+
Assists.service?.startActivity(this)
91+
}
92+
//步骤1执行完,紧接执行步骤2
93+
StepManager.execute(this::class.java, 2)
94+
}.next(2) {
95+
//步骤2逻辑
96+
//查找通讯录按钮
97+
UIOperate.findByText("通讯录").forEach {
98+
//获取到按钮后执行其他逻辑
99+
}
100+
}
101+
}
102+
}
103+
```
104+
2. 在执行前注册上面步骤实现类`OpenWechat`
105+
106+
```kotlin
107+
StepManager.register(OpenWechat::class.java)
108+
```
109+
3. 开始执行`执行前请确保无障碍服务已开启`(开始执行请使用`beginExecute()`,后续的步骤执行请使用`execute()`方法)
110+
111+
```kotlin
112+
//从步骤1开始执行
113+
StepManager.beginExecute(OpenWechat::class.java, 1)
114+
```
115+
具体使用可以看我的[Demo](https://github.com/ven-coder/assists)

assists/src/main/java/com/ven/assist/Assists.kt

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
package com.ven.assist
22

3-
import android.content.Context
43
import android.content.Intent
54
import android.provider.Settings
65
import android.view.accessibility.AccessibilityEvent
7-
import android.view.accessibility.AccessibilityManager
86
import com.blankj.utilcode.util.ActivityUtils
97
import com.blankj.utilcode.util.LogUtils
8+
import com.ven.assist.step.StepOperator
109

1110
object Assists {
1211
/**
@@ -41,13 +40,8 @@ object Assists {
4140
/**
4241
*检查无障碍服务是否开启
4342
*/
44-
fun isAccessibilityServiceEnabled(context: Context, serviceName: String): Boolean {
45-
val am = context.getSystemService(Context.ACCESSIBILITY_SERVICE) as AccessibilityManager
46-
val enabledServices = Settings.Secure.getString(
47-
context.contentResolver,
48-
Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES
49-
)
50-
return enabledServices?.contains(serviceName) == true
43+
fun isAccessibilityServiceEnabled(): Boolean {
44+
return service != null
5145
}
5246

5347
/**
@@ -93,6 +87,8 @@ object Assists {
9387
interface StepListener {
9488
fun onStepStart() {}
9589
fun onStepStop() {}
90+
fun onStep(step: StepOperator) {}
91+
fun onLoop(step: StepOperator) {}
9692
}
9793

9894
/**

assists/src/main/java/com/ven/assist/step/StepCollector.kt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package com.ven.assist.step
22

3+
/**
4+
* 步骤收集器
5+
*/
36
class StepCollector(private val clazzName: String) {
47

58
private val stepOperatorMap: HashMap<Int, StepOperator> = hashMapOf()
@@ -8,6 +11,11 @@ class StepCollector(private val clazzName: String) {
811
stepOperatorMap[step]?.let { return it } ?: throw RuntimeException("The class $clazzName does not have an implementation logic for Step $step. / 类${clazzName}中的没有步骤${step}的实现逻辑")
912
}
1013

14+
/**
15+
* 单次步骤
16+
* @param step 步骤序号
17+
* @param next 步骤逻辑接口
18+
*/
1119
fun next(
1220
step: Int,
1321
next: (stepOperator: StepOperator) -> Unit
@@ -19,6 +27,12 @@ class StepCollector(private val clazzName: String) {
1927
return this
2028
}
2129

30+
/**
31+
* 循环步骤
32+
* @param step 步骤序号
33+
* @param loopDuration 循环时长
34+
* @param next 步骤逻辑接口,接口中需要返回[kotlin.Boolean],false继续循环,true终止循环
35+
*/
2236
fun nextLoop(
2337
step: Int,
2438
loopDuration: Long = 5000,

assists/src/main/java/com/ven/assist/step/StepOperator.kt

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ class StepOperator(
3131
Assists.ListenerManager.stepListener.forEach { it.onStepStop() }
3232
return@runOnUiThreadDelayed
3333
}
34-
it.invoke(this@StepOperator)
34+
onStep(it)
3535
}, delay)
3636
} else {
3737
ThreadUtils.runOnUiThreadDelayed({
@@ -47,7 +47,8 @@ class StepOperator(
4747
loopSurplusTime = millisUntilFinished
4848
loopSurplusSecond = millisUntilFinished / 1000f
4949
if (StepManager.isStop) Assists.ListenerManager.stepListener.forEach { it.onStepStop() }
50-
if (it.invoke(this@StepOperator) || StepManager.isStop) {
50+
Assists.ListenerManager.stepListener.forEach { it.onLoop(this@StepOperator) }
51+
if (onStep(it) || StepManager.isStop) {
5152
cancel()
5253
loopDownTimer = null
5354
}
@@ -56,7 +57,7 @@ class StepOperator(
5657
override fun onFinish() {
5758
loopSurplusTime = 0
5859
loopSurplusSecond = 0f
59-
it.invoke(this@StepOperator)
60+
onStep(it)
6061
}
6162

6263
}
@@ -67,4 +68,9 @@ class StepOperator(
6768
LogUtils.e("The execution logic for Step [$step] in the class [${clazzName}] has not been implemented. / 类[${clazzName}]中的步骤[${step}]未实现执行逻辑")
6869
}
6970
}
71+
72+
private fun onStep(it: (stepOperator: StepOperator) -> Boolean): Boolean {
73+
Assists.ListenerManager.stepListener.forEach { it.onStep(this) }
74+
return it.invoke(this)
75+
}
7076
}

graphics/ic_launcher_round.png

18.2 KB
Loading

simple/build.gradle

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,6 @@ android {
1111
versionCode 1
1212
versionName "1.0.0"
1313
testInstrumentationRunner 'androidx.test.runner.AndroidJUnitRunner'
14-
//数据库自动迁移空间配置
15-
javaCompileOptions {
16-
annotationProcessorOptions {
17-
arguments = ["room.schemaLocation": "$projectDir/schemas".toString()]
18-
}
19-
}
2014
}
2115
buildTypes {
2216
release {
@@ -54,4 +48,5 @@ dependencies {
5448
implementation "androidx.room:room-runtime:2.4.3"
5549
implementation 'androidx.room:room-ktx:2.4.3'
5650
implementation 'com.github.mrmike:ok2curl:0.8.0'
51+
// implementation 'com.github.ven-coder:assists:1.0.0'
5752
}

simple/src/main/AndroidManifest.xml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,6 @@
5151
<category android:name="android.intent.category.LAUNCHER" />
5252
</intent-filter>
5353
</activity>
54-
<activity android:name=".LoginActivity" />
55-
5654
<service
5755
android:name="com.ven.assist.AssistsService"
5856
android:enabled="true"

simple/src/main/java/com/ven/assists/simple/MainActivity.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class MainActivity : AppCompatActivity(), Assists.ListenerManager.ServiceListene
1414
val viewBind: ActivityMainBinding by lazy {
1515
ActivityMainBinding.inflate(layoutInflater).apply {
1616
btnOption.setOnClickListener {
17-
if (Assists.isAccessibilityServiceEnabled(this@MainActivity, AssistsService::javaClass.name)) {
17+
if (Assists.isAccessibilityServiceEnabled()) {
1818
OverManager.mainOver?.show()
1919
} else {
2020
Assists.openAccessibilitySetting()
@@ -29,7 +29,7 @@ class MainActivity : AppCompatActivity(), Assists.ListenerManager.ServiceListene
2929
}
3030

3131
private fun checkServiceEnable() {
32-
if (Assists.isAccessibilityServiceEnabled(this, AssistsService::javaClass.name)) {
32+
if (Assists.isAccessibilityServiceEnabled()) {
3333
viewBind.btnOption.text = "显示操作浮窗"
3434
} else {
3535
viewBind.btnOption.text = "开启服务"

0 commit comments

Comments
 (0)