Skip to content

Commit

Permalink
reduce toast frequency
Browse files Browse the repository at this point in the history
  • Loading branch information
zfdang committed Oct 26, 2020
1 parent 78b0a2e commit 232bec0
Showing 1 changed file with 20 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class TouchHelperServiceImpl {

Expand All @@ -68,6 +70,7 @@ public class TouchHelperServiceImpl {
private Map<String, Set<ActivityWidgetDescription>> mapActivityWidgets;
private Set<ActivityWidgetDescription> setWidgets;
private PackagePositionDescription packagePositionDescription;
private ReentrantLock toastLock = new ReentrantLock();

public TouchHelperServiceImpl(AccessibilityService service) {
this.service = service;
Expand Down Expand Up @@ -400,14 +403,15 @@ private void findSkipButtonByTextOrDescription(AccessibilityNodeInfo root) {
ArrayList<AccessibilityNodeInfo> listB = new ArrayList<>();
listA.add(root);

// showAllChildren(root);

int total = listA.size();
int index = 0;
while (index < total) {
AccessibilityNodeInfo node = listA.get(index++);
if (node != null) {
CharSequence description = node.getContentDescription();
CharSequence text = node.getText();
// Log.d(TAG, Utilities.describeAccessibilityNode(node));

// try to find keyword
for (String keyword: keyWordList) {
Expand All @@ -421,13 +425,17 @@ private void findSkipButtonByTextOrDescription(AccessibilityNodeInfo root) {

if (isFind) {
ShowToastInIntentService("正在根据关键字跳过广告...");
// Log.d(TAG, Utilities.describeAccessibilityNode(node));
// Log.d(TAG, "keyword = " + keyword);

if (!node.performAction(AccessibilityNodeInfo.ACTION_CLICK)) {
if (!node.getParent().performAction(AccessibilityNodeInfo.ACTION_CLICK)) {
Rect rect = new Rect();
node.getBoundsInScreen(rect);
click(rect.centerX(), rect.centerY(), 0, 20);
}
}
break;
}
}
for (int n = 0; n < node.getChildCount(); n++) {
Expand Down Expand Up @@ -504,6 +512,14 @@ private void findSkipButtonByWidget(AccessibilityNodeInfo root, Set<ActivityWidg
}
}


private void showAllChildren(AccessibilityNodeInfo root){
ArrayList<AccessibilityNodeInfo> roots = new ArrayList<>();
roots.add(root);
ArrayList<AccessibilityNodeInfo> nodeList = new ArrayList<>();
findAllNode(roots, nodeList, "");
}

/**
* 查找所有的控件
*/
Expand Down Expand Up @@ -572,6 +588,7 @@ private void stopSkipAdProcess() {
b_method_by_button_keyword = false;
setWidgets = null;
packagePositionDescription = null;
if(toastLock.isLocked()){ toastLock.unlock(); }
if( !futureExpireSkipAdProcess.isCancelled() && !futureExpireSkipAdProcess.isDone()) {
futureExpireSkipAdProcess.cancel(true);
}
Expand Down Expand Up @@ -886,7 +903,8 @@ public void onClick(View v) {

public void ShowToastInIntentService(final String sText) {
final Context myContext = this.service;
if(mSetting.isSkipAdNotification()) {
// show one toast in 5 seconds only
if(mSetting.isSkipAdNotification() && toastLock.tryLock()) {
new Handler(Looper.getMainLooper()).post(new Runnable() {
@Override
public void run() {
Expand Down

0 comments on commit 232bec0

Please sign in to comment.