22
33import android .accessibilityservice .AccessibilityService ;
44import android .accessibilityservice .GestureDescription ;
5+ import android .annotation .SuppressLint ;
56import android .app .Notification ;
67import android .app .PendingIntent ;
78import android .content .ComponentName ;
89import android .content .SharedPreferences ;
910import android .content .pm .PackageManager ;
11+ import android .graphics .Path ;
1012import android .graphics .Rect ;
1113import android .os .Bundle ;
1214import android .os .Parcelable ;
13- import android .graphics .Path ;
1415import android .preference .PreferenceManager ;
16+ import android .util .DisplayMetrics ;
1517import android .util .Log ;
1618import android .view .accessibility .AccessibilityEvent ;
1719import android .view .accessibility .AccessibilityNodeInfo ;
18- import android .util .DisplayMetrics ;
19- import xyz .monkeytong .hongbao .utils .HongbaoSignature ;
20- import xyz .monkeytong .hongbao .utils .PowerUtil ;
2120
2221import java .util .List ;
2322
23+ import xyz .monkeytong .hongbao .utils .HongbaoSignature ;
24+ import xyz .monkeytong .hongbao .utils .PowerUtil ;
25+
2426public class HongbaoService extends AccessibilityService implements SharedPreferences .OnSharedPreferenceChangeListener {
2527 private static final String TAG = "HongbaoService" ;
2628 private static final String WECHAT_DETAILS_EN = "Details" ;
@@ -35,6 +37,7 @@ public class HongbaoService extends AccessibilityService implements SharedPrefer
3537 private static final String WECHAT_LUCKMONEY_DETAIL_ACTIVITY = "LuckyMoneyDetailUI" ;
3638 private static final String WECHAT_LUCKMONEY_GENERAL_ACTIVITY = "LauncherUI" ;
3739 private static final String WECHAT_LUCKMONEY_CHATTING_ACTIVITY = "ChattingUI" ;
40+ private static final String WECHAT_LUCKMONEY_RECEIVE_UI_ACTIVITY = "LuckyMoneyReceiveUI" ;
3841 private String currentActivityName = WECHAT_LUCKMONEY_GENERAL_ACTIVITY ;
3942
4043 private AccessibilityNodeInfo rootNodeInfo , mReceiveNode , mUnpackNode ;
@@ -74,8 +77,6 @@ public void onAccessibilityEvent(AccessibilityEvent event) {
7477 private void watchChat (AccessibilityEvent event ) {
7578 this .rootNodeInfo = getRootInActiveWindow ();
7679
77- if (rootNodeInfo == null ) return ;
78-
7980 mReceiveNode = null ;
8081 mUnpackNode = null ;
8182
@@ -92,7 +93,7 @@ private void watchChat(AccessibilityEvent event) {
9293 }
9394 /* 如果戳开但还未领取 */
9495 Log .d (TAG , "戳开红包!" + " mUnpackCount: " + mUnpackCount + " mUnpackNode: " + mUnpackNode );
95- if (mUnpackCount >= 1 && ( mUnpackNode != null )) {
96+ if (canOpen ( event )) {
9697 int delayFlag = sharedPreferences .getInt ("pref_open_delay" , 0 ) * 1000 ;
9798 new android .os .Handler ().postDelayed (
9899 new Runnable () {
@@ -110,41 +111,41 @@ public void run() {
110111 }
111112 }
112113
114+ private boolean canOpen (AccessibilityEvent event ) {
115+ return event .getClassName () != null
116+ && event .getClassName ().toString ().contains (WECHAT_LUCKMONEY_RECEIVE_UI_ACTIVITY )
117+ && event .isFullScreen ()
118+ && event .getEventType () == AccessibilityEvent .TYPE_WINDOW_STATE_CHANGED ;
119+ }
120+
113121 private void openPacket () {
114- DisplayMetrics metrics = getResources ().getDisplayMetrics ();
115- float dpi = metrics .densityDpi ;
116- Log .d (TAG , "openPacket!" + dpi );
117122 if (android .os .Build .VERSION .SDK_INT <= 23 ) {
118- mUnpackNode .performAction (AccessibilityNodeInfo .ACTION_CLICK );
119- } else {
120- if (android .os .Build .VERSION .SDK_INT > 23 ) {
121- Path path = new Path ();
122- if (640 == dpi ) { //1440
123- path .moveTo (720 , 1575 );
124- } else if (320 == dpi ){//720p
125- path .moveTo (355 , 780 );
126- }else if (480 == dpi ){//1080p
127- path .moveTo (533 , 1115 );
123+ if (mUnpackCount >= 1 && (mUnpackNode != null )) {
124+ mUnpackNode .performAction (AccessibilityNodeInfo .ACTION_CLICK );
125+ }
126+ }else {
127+ DisplayMetrics metrics = getResources ().getDisplayMetrics ();
128+ float dpi = metrics .densityDpi ;
129+ Log .d (TAG , "openPacket!" + dpi );
130+ Path path = new Path ();
131+ path .moveTo (metrics .widthPixels * 0.5f , metrics .heightPixels * 0.6f );
132+ GestureDescription .Builder builder = new GestureDescription .Builder ();
133+ GestureDescription gestureDescription = builder .addStroke (new GestureDescription .StrokeDescription (path , 450 , 50 )).build ();
134+ dispatchGesture (gestureDescription , new GestureResultCallback () {
135+ @ Override
136+ public void onCompleted (GestureDescription gestureDescription ) {
137+ Log .d (TAG , "onCompleted" );
138+ mMutex = false ;
139+ super .onCompleted (gestureDescription );
128140 }
129- GestureDescription .Builder builder = new GestureDescription .Builder ();
130- GestureDescription gestureDescription = builder .addStroke (new GestureDescription .StrokeDescription (path , 450 , 50 )).build ();
131- dispatchGesture (gestureDescription , new GestureResultCallback () {
132- @ Override
133- public void onCompleted (GestureDescription gestureDescription ) {
134- Log .d (TAG , "onCompleted" );
135- mMutex = false ;
136- super .onCompleted (gestureDescription );
137- }
138-
139- @ Override
140- public void onCancelled (GestureDescription gestureDescription ) {
141- Log .d (TAG , "onCancelled" );
142- mMutex = false ;
143- super .onCancelled (gestureDescription );
144- }
145- }, null );
146141
147- }
142+ @ Override
143+ public void onCancelled (GestureDescription gestureDescription ) {
144+ Log .d (TAG , "onCancelled" );
145+ mMutex = false ;
146+ super .onCancelled (gestureDescription );
147+ }
148+ }, null );
148149 }
149150 }
150151
0 commit comments