@@ -67,6 +67,7 @@ public class TouchHelperServiceImpl {
6767 private String currentPackageName , currentActivityName ;
6868 private String packageName ;
6969 private Set <String > setPackages , setIMEApps , setHomes , setWhiteList ;
70+ private Set <String > clickedWidgets ;
7071 private List <String > keyWordList ;
7172
7273 private Map <String , PackagePositionDescription > mapPackagePositions ;
@@ -131,6 +132,9 @@ public void onServiceConnected() {
131132 packageManager = service .getPackageManager ();
132133 updatePackage ();
133134
135+ // init clickedWidgets
136+ clickedWidgets = new HashSet <String >();
137+
134138 // install receiver and handler for broadcasting events
135139 InstallReceiverAndHandler ();
136140
@@ -191,7 +195,7 @@ public boolean handleMessage(Message msg) {
191195 break ;
192196 case TouchHelperService .ACTION_START_SKIPAD :
193197// Log.d(TAG, "resume from wakeup and start to skip ads now ...");
194- findSkipButtonByTextOrDescription ( service . getRootInActiveWindow () );
198+ startSkipAdProcess ( );
195199 break ;
196200 }
197201 return true ;
@@ -368,6 +372,8 @@ public void onUnbind(Intent intent) {
368372 * 查找并点击包含keyword控件,目标包括Text和Description
369373 * * */
370374 private void findSkipButtonByTextOrDescription (AccessibilityNodeInfo root ) {
375+ // Log.d(TAG, "findSkipButtonByTextOrDescription triggered: " + Utilities.describeAccessibilityNode(root));
376+
371377 ArrayList <AccessibilityNodeInfo > listA = new ArrayList <>();
372378 ArrayList <AccessibilityNodeInfo > listB = new ArrayList <>();
373379 listA .add (root );
@@ -391,22 +397,38 @@ private void findSkipButtonByTextOrDescription(AccessibilityNodeInfo root) {
391397 } else if (description != null && (description .toString ().length () <= keyword .length () + 6 ) && description .toString ().contains (keyword )) {
392398 isFind = true ;
393399 }
400+ if (isFind ) {
401+ // if this node matches our target, stop finding more keywords
402+ // Log.d(TAG, "identify keyword = " + keyword);
403+ break ;
404+ }
405+ }
394406
395- if (isFind ) {
396- ShowToastInIntentService ("正在根据关键字跳过广告..." );
397- // Log.d(TAG, Utilities.describeAccessibilityNode(node));
398- // Log.d(TAG, "keyword = " + keyword);
407+ // if this node matches our target, try to click it
408+ if (isFind ) {
409+ String nodeDesc = Utilities .describeAccessibilityNode (node );
410+ // Log.d(TAG, nodeDesc);
411+ if (!clickedWidgets .contains (nodeDesc )){
412+ clickedWidgets .add (nodeDesc );
399413
414+ ShowToastInIntentService ("正在根据关键字跳过广告..." );
400415 boolean clicked = node .performAction (AccessibilityNodeInfo .ACTION_CLICK );
401416// Log.d(TAG, "self clicked = " + clicked);
402417 if (!clicked ) {
403418 Rect rect = new Rect ();
404419 node .getBoundsInScreen (rect );
405420 click (rect .centerX (), rect .centerY (), 0 , 20 );
406421 }
407- break ;
422+
423+ // is it possible that there are more nodes to click and this node does not work?
424+ // don't stop looking for more nodes
425+ // break;
426+ } else {
427+ // Log.d(TAG, "Clicked already:" + nodeDesc);
408428 }
409429 }
430+
431+ // find all children nodes
410432 for (int n = 0 ; n < node .getChildCount (); n ++) {
411433 listB .add (node .getChild (n ));
412434 }
@@ -451,20 +473,26 @@ private void findSkipButtonByWidget(AccessibilityNodeInfo root, Set<PackageWidge
451473 } else if (cText != null && !e .text .isEmpty () && cText .toString ().contains (e .text )) {
452474 isFind = true ;
453475 }
476+
454477 if (isFind ) {
455478// Log.d(TAG, "Find skip-ad by Widget " + e.toString());
456- ShowToastInIntentService ("正在根据控件跳过广告..." );
457- if (e .onlyClick ) {
458- click (temRect .centerX (), temRect .centerY (), 0 , 20 );
459- } else {
460- if (!node .performAction (AccessibilityNodeInfo .ACTION_CLICK )) {
461- if (!node .getParent ().performAction (AccessibilityNodeInfo .ACTION_CLICK )) {
462- click (temRect .centerX (), temRect .centerY (), 0 , 20 );
479+ String nodeDesc = Utilities .describeAccessibilityNode (node );
480+ if (!clickedWidgets .contains (nodeDesc )) {
481+ clickedWidgets .add (nodeDesc );
482+
483+ ShowToastInIntentService ("正在根据控件跳过广告..." );
484+ if (e .onlyClick ) {
485+ click (temRect .centerX (), temRect .centerY (), 0 , 20 );
486+ } else {
487+ if (!node .performAction (AccessibilityNodeInfo .ACTION_CLICK )) {
488+ if (!node .getParent ().performAction (AccessibilityNodeInfo .ACTION_CLICK )) {
489+ click (temRect .centerX (), temRect .centerY (), 0 , 20 );
490+ }
463491 }
464492 }
493+ setWidgets = null ;
494+ return ;
465495 }
466- setWidgets = null ;
467- return ;
468496 }
469497 }
470498 for (int n = 0 ; n < node .getChildCount (); n ++) {
@@ -531,6 +559,7 @@ private void startSkipAdProcess() {
531559 b_method_by_button_keyword = true ;
532560 setWidgets = null ;
533561 packagePositionDescription = null ;
562+ clickedWidgets .clear ();
534563
535564 // cancel all methods 4 seconds later
536565 if ( !futureExpireSkipAdProcess .isCancelled () && !futureExpireSkipAdProcess .isDone ()) {
0 commit comments