Skip to content
This repository has been archived by the owner on Jul 30, 2020. It is now read-only.

Commit

Permalink
Fix some bugs:
Browse files Browse the repository at this point in the history
1. 引入了mutex互斥,防止多处回调同时进入同一阶段引发的混乱。
2. 当待抢队列清空后,抢先调用handler防止return后后续流程被挂起。

现在自定义名称的红包成功率已接近100%,id重复时的问题仍未很好地解决。
  • Loading branch information
ZhongyiTong committed Oct 6, 2015
1 parent 1db8597 commit 33e5e43
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 15 deletions.
49 changes: 34 additions & 15 deletions app/src/main/java/com/miui/hongbao/HongbaoService.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import android.view.accessibility.AccessibilityNodeInfo;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
Expand Down Expand Up @@ -36,7 +37,6 @@ public class HongbaoService extends AccessibilityService {
*/
private int ttl = 0;


/**
* AccessibilityEvent的回调方法
* <p/>
Expand All @@ -48,31 +48,47 @@ public class HongbaoService extends AccessibilityService {
@Override
public void onAccessibilityEvent(AccessibilityEvent event) {

AccessibilityNodeInfo nodeInfo = event.getSource();
if (Stage.getInstance().mutex) return;

Stage.getInstance().mutex = true;

try {
handleWindowChange(event.getSource());
} finally {
Stage.getInstance().mutex = false;
}

}

@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
private void handleWindowChange(AccessibilityNodeInfo nodeInfo) {
switch (Stage.getInstance().getCurrentStage()) {
case Stage.OPENING_STAGE:
Log.d("TTL", String.valueOf(ttl));
/* 如果打开红包失败且还没到达最大尝试次数,重试 */
// 调试信息,打印TTL
// Log.d("TTL", String.valueOf(ttl));

/* 如果打开红包失败且还没到达最大尝试次数,重试 */
if (openHongbao(nodeInfo) == -1 && ttl < MAX_TTL) return;

if (Stage.getInstance().getCurrentStage() == Stage.OPENED_STAGE) {
List<AccessibilityNodeInfo> successNodes = nodeInfo.findAccessibilityNodeInfosByText("红包详情");
if (successNodes.isEmpty()) return;
}
ttl = 0;
Stage.getInstance().entering(Stage.FETCHED_STAGE);
performGlobalAction(GLOBAL_ACTION_BACK);
performMyGlobalAction(GLOBAL_ACTION_BACK);
if (nodesToFetch.size() == 0) handleWindowChange(nodeInfo);
break;
case Stage.OPENED_STAGE:

List<AccessibilityNodeInfo> successNodes = nodeInfo.findAccessibilityNodeInfosByText("红包详情");
if (successNodes.isEmpty() && ttl < MAX_TTL) {
ttl += 1;
return;
}
ttl = 0;
Stage.getInstance().entering(Stage.FETCHED_STAGE);
performGlobalAction(GLOBAL_ACTION_BACK);
performMyGlobalAction(GLOBAL_ACTION_BACK);
break;
case Stage.FETCHED_STAGE:
/* 先消灭待抢红包队列中的红包 */
/* 先消灭待抢红包队列中的红包 */
if (nodesToFetch.size() > 0) {
/* 从最下面的红包开始戳 */
/* 从最下面的红包开始戳 */
AccessibilityNodeInfo node = nodesToFetch.remove(nodesToFetch.size() - 1);
if (node.getParent() != null) {
String id = getHongbaoHash(node);
Expand All @@ -95,7 +111,6 @@ public void onAccessibilityEvent(AccessibilityEvent event) {
Stage.getInstance().entering(Stage.FETCHED_STAGE);
break;
}

}


Expand Down Expand Up @@ -216,5 +231,9 @@ public void onInterrupt() {

}


@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
public void performMyGlobalAction(int action) {
Stage.getInstance().mutex = false;
performGlobalAction(action);
}
}
6 changes: 6 additions & 0 deletions app/src/main/java/com/miui/hongbao/Stage.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ public class Stage {
*/
private int currentStage = FETCHED_STAGE;

/**
* 阶段互斥,不允许多次回调进入同一阶段
*/
public boolean mutex = false;

/**
* 单例设计,防止通过构造函数创建对象
*/
Expand All @@ -44,6 +49,7 @@ public static Stage getInstance() {
*/
public void entering(int _stage) {
stageInstance.currentStage = _stage;
mutex = false;
}

/**
Expand Down

0 comments on commit 33e5e43

Please sign in to comment.