Skip to content

Commit 51d65ed

Browse files
committed
update readme commit sample code
1 parent 95495e8 commit 51d65ed

4 files changed

Lines changed: 127 additions & 14 deletions

File tree

README.md

Lines changed: 42 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,35 @@
55

66
[![](https://jitpack.io/v/SmartDengg/asm-clickdebounce.svg)](https://jitpack.io/#SmartDengg/asm-clickdebounce)
77

8-
It is a gradle plugin that uses bytecode weaving technology to solve the click debounce problem of Android applications.
8+
### Support incremental compilation! Support parallel compilation! Faster compilation speed, and shorter compilation time.
99

10-
Safe, efficient, easy to use, support incremental build to avoid waste of build time.
10+
It is a gradle plugin that uses bytecode weaving technology to solve the click jitter problem of Android applications.
1111

12-
I also wrote a **[blog](https://www.jianshu.com/p/28751130c038)** to share my ideas to solve the click debounce.
12+
For example, a normal `onClick` method without any debounce plan, multiple quick clicks may trigger multiple Activity starts:
13+
14+
```java
15+
@Override public void onClick(View v) {
16+
startActivity(new Intent(MainActivity.this, SecondActivity.class));
17+
}
18+
```
19+
20+
modify the bytecode at compile time to:
21+
22+
```java
23+
@Debounced
24+
public void onClick(View var1) {
25+
if (DebouncedPredictor.shouldDoClick(var1)) {
26+
startActivity(new Intent(this, SecondActivity.class));
27+
}
28+
}
29+
```
30+
31+
The `@Debounced` annotation indicates that the method has been debounced. The `shouldDoClick(View)` method will determine which are the jitters and which are the correct clicks.
32+
33+
I also wrote a **[BLOG](https://www.jianshu.com/p/28751130c038)** to share my ideas to solve the click jitter.
34+
35+
*Note: This repository is just a gradle plugin, responsible for bytecode weaving work. Android runtime library please move [here](https://github.com/SmartDengg/asm-clickdebounce-runtime).*
1336

14-
*Note: This repository is just a gradle plugin, responsible for bytecode weaving work, Android runtime library please move [here](https://github.com/SmartDengg/asm-clickdebounce-runtime).*
1537

1638
## Requirements
1739

@@ -50,7 +72,7 @@ buildscript {
5072

5173
**Step 2**. Apply it in your module:
5274

53-
*It supports 'com.android.application', 'com.android.library' and 'com.android.feature'*.
75+
Supports 'com.android.application', 'com.android.library' and 'com.android.feature'.
5476

5577
```groovy
5678
@@ -59,16 +81,24 @@ apply plugin: 'smartdengg.clickdebounce'
5981
6082
```
6183

84+
**Step 3 (Optional)**. By adding the following code to your `build.gradle` to enable printe the beautiful log or add an exclusive list to indicate which methods do not need to be debounced. By default, the log is not printed, and process all the methods in the [support](#jump) list.
6285

63-
**Step 3 (Optional)**. Enable logging by adding the following code to your build.gradle:
86+
**It is not recommended to manually add @Debounce annotations to methods, you should use the exclusive feature.**
6487

6588
```groovy
6689
67-
debounce.loggable = true
90+
debounce {
91+
// logable
92+
loggable = true
93+
// asm description [class: [methods]]
94+
exclusion = ["com/smartdengg/clickdebounce/ClickProxy": ['onClick(Landroid/view/View;)V',
95+
'onItemClick(Landroid/widget/AdapterView;Landroid/view/View;IJ)V']]
96+
}
6897
6998
```
7099

71-
**Step 4 (Optional)**. Set the debounce window time(default is 300 milliseconds):
100+
101+
**Step 4 (Optional)**. Set the debounce window time in your Java code(default is 300 milliseconds):
72102

73103
```java
74104

@@ -84,7 +114,7 @@ These file path is located in **buildDir/outputs/debounce/logs/<variant>/**, as
84114

85115
```
86116
.
87-
+-- app(Apply this AGP)
117+
+-- app (apply this AGP)
88118
| +-- build
89119
| +-- generated
90120
| +-- intermediates
@@ -102,10 +132,8 @@ These file path is located in **buildDir/outputs/debounce/logs/<variant>/**, as
102132
103133
```
104134

105-
- **files.txt** :Record the class files consumed by this build,it can help you better understand the incremental build
106-
- **classes.txt** :Record information about the classes and methods weaved in this build
107-
108-
135+
- **files.txt** :Record the class files consumed by this build,it can help you better understand this build.
136+
- **classes.txt** :Record information about the classes and methods weaved in this build.
109137

110138

111139
## How it works
@@ -114,7 +142,7 @@ These file path is located in **buildDir/outputs/debounce/logs/<variant>/**, as
114142

115143
![](art/clickdebounce.png)
116144

117-
## Support
145+
## <span id="jump">Support</span>
118146

119147
- [x] [View.OnClickListener](https://developer.android.com/reference/android/view/View.OnClickListener)
120148
- [x] [AdapterView.OnItemClickListener](https://developer.android.com/reference/android/widget/AdapterView.OnItemClickListener)
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.smartdengg.plugin
2+
3+
/**
4+
* 创建时间: 2019/06/03 12:13 <br>
5+
* 作者: SmartDengg <br>
6+
* 描述:
7+
* */
8+
class DebounceExtension {
9+
static final String NAME = "debounce"
10+
boolean loggable
11+
Map<String, List<String>> exclusion = [:]
12+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.smartdengg.plugin
2+
3+
import com.google.common.base.Preconditions
4+
import com.google.common.collect.Sets
5+
6+
import java.util.concurrent.ForkJoinPool
7+
import java.util.concurrent.ForkJoinTask
8+
9+
/**
10+
* 创建时间: 2019/06/04 18:43 <br>
11+
* 作者: SmartDengg <br>
12+
* 描述: */
13+
@Singleton
14+
class ForkJoinExecutor {
15+
16+
ForkJoinPool forkJoinPool = new ForkJoinPool()
17+
private final Set<ForkJoinTask<?>> futureSet = Sets.newConcurrentHashSet()
18+
19+
void execute(Closure task) {
20+
ForkJoinTask<?> submitted = forkJoinPool.submit(new Runnable() {
21+
@Override
22+
void run() {
23+
task.call()
24+
}
25+
})
26+
boolean added = futureSet.add(submitted)
27+
Preconditions.checkState(added, "Failed to add task")
28+
}
29+
30+
void waitingForAllTasks() {
31+
try {
32+
for (Iterator iterator = futureSet.iterator(); iterator.hasNext();) {
33+
ForkJoinTask<?> task = iterator.next()
34+
task.join()
35+
iterator.remove()
36+
}
37+
} finally {
38+
}
39+
}
40+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.smartdengg.clickdebounce;
2+
3+
import android.util.Log;
4+
import android.view.View;
5+
import android.widget.AdapterView;
6+
7+
/**
8+
* 创建时间: 2019/06/03 11:58 <br>
9+
* 作者: SmartDengg <br>
10+
* 描述:
11+
*/
12+
public class ClickProxy implements View.OnClickListener, AdapterView.OnItemClickListener {
13+
14+
private static final String TAG = ClickProxy.class.getSimpleName();
15+
private static volatile ClickProxy instance;
16+
17+
static ClickProxy getInstance() {
18+
synchronized (ClickProxy.class) {
19+
if (instance == null) {
20+
instance = new ClickProxy();
21+
}
22+
return instance;
23+
}
24+
}
25+
26+
@Override public void onClick(View v) {
27+
Log.d(TAG, "onClick : " + this.getClass().getName());
28+
}
29+
30+
@Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
31+
Log.d(TAG, "onClick : " + this.getClass().getName());
32+
}
33+
}

0 commit comments

Comments
 (0)