AutoReplyService.java 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742
  1. package com.wiipu.marketingrobot;
  2. import android.accessibilityservice.AccessibilityService;
  3. import android.accessibilityservice.AccessibilityServiceInfo;
  4. import android.annotation.TargetApi;
  5. import android.app.ActivityManager;
  6. import android.app.KeyguardManager;
  7. import android.app.Notification;
  8. import android.app.PendingIntent;
  9. import android.content.ComponentName;
  10. import android.content.Context;
  11. import android.content.Intent;
  12. import android.content.SharedPreferences;
  13. import android.content.pm.PackageInfo;
  14. import android.content.pm.PackageManager;
  15. import android.os.Build;
  16. import android.os.PowerManager;
  17. import android.os.SystemClock;
  18. import android.text.TextUtils;
  19. import android.view.accessibility.AccessibilityEvent;
  20. import android.view.accessibility.AccessibilityManager;
  21. import android.view.accessibility.AccessibilityNodeInfo;
  22. import com.wiipu.marketingrobot.contract.getCommonValueContract;
  23. import com.wiipu.marketingrobot.function.addAutoReplyFunction;
  24. import com.wiipu.marketingrobot.function.addFriendFunction;
  25. import com.wiipu.marketingrobot.function.addGroupFunction;
  26. import com.wiipu.marketingrobot.function.eventClassFunction;
  27. import com.wiipu.marketingrobot.function.getMoneyFunction;
  28. import com.wiipu.marketingrobot.function.handleChatFunction;
  29. import com.wiipu.marketingrobot.presenter.getCommonValuePresenter;
  30. import com.wiipu.marketingrobot.response.CommonValueResponse;
  31. import com.wiipu.marketingrobot.response.enableAddFriendResponse;
  32. import com.wiipu.marketingrobot.widget.AccessibilityHelper;
  33. import com.wiipu.netlib.rxbus.RxBus;
  34. import com.wiipu.netlib.utils.LogUtils;
  35. import java.util.ArrayList;
  36. import java.util.Iterator;
  37. import java.util.List;
  38. import rx.Subscription;
  39. import rx.functions.Action1;
  40. import rx.subscriptions.CompositeSubscription;
  41. /**
  42. * Recoder: Axion
  43. * Description: 智能投股机器人主服务
  44. */
  45. public class AutoReplyService extends AccessibilityService implements getCommonValueContract.View {
  46. private final static String MM_PNAME = "com.tencent.mm";
  47. private static final String TAG = "maptrix";
  48. public static boolean hasAction = false;
  49. private String scontent;
  50. private static PackageInfo mWechatPackageInfo = null;
  51. boolean isGroupChat = false;
  52. public static boolean isInAction = false;
  53. public static ArrayList<String> msgList = new ArrayList<>();//事件列表
  54. public static String msgSenderName;
  55. private getMoneyFunction mGetMoney;
  56. private handleChatFunction mHandleChat;
  57. private addFriendFunction mAddFriendFunction;
  58. private addGroupFunction mAddGroupFunction;
  59. public static AutoReplyService mAutoReplyService;
  60. private addAutoReplyFunction mAddAutoReplyFunction;
  61. private CompositeSubscription mSubscription = new CompositeSubscription();
  62. private getCommonValuePresenter mGetCommonValuePresenter;
  63. public volatile static int enableFriendsCounts = 1;//为了防止程序初次运行时候出现bug,先置1
  64. private SharedPreferences pref;
  65. private boolean breakOutLoop = false;
  66. private static final String savedRobotNum = "robotNum";
  67. private static final String savedRobotId = "robotId";
  68. public static String allowedArea;
  69. private boolean inMineView = false; //标志页面在主页面“我的”模块
  70. private boolean msgView = false;//标志收到消息
  71. private AccessibilityEvent changeStateEvent = AccessibilityEvent.obtain();
  72. private AccessibilityEvent doInBackEvent = AccessibilityEvent.obtain();
  73. private AccessibilityEvent webViewEvent = AccessibilityEvent.obtain();
  74. private boolean undoWorkFlag = false;
  75. //temp
  76. public static boolean isInSeeRoomInfoUICanNotGoBack = false;
  77. public static boolean isSendingPermissionRequest = false;
  78. public enum actionType {
  79. isHongBao, isZhuanZhang, isChat, isAddFriend, isAddAutoReply, isNone, isGroupInvited
  80. }
  81. public static actionType mActionType = actionType.isNone;
  82. public enum windowAction {
  83. WINDOW_NONE, WINDOW_LUCKYMONEY_RECEIVEUI, WINDOW_LUCKYMONEY_DETAIL, WINDOW_LAUNCHER, WINDOW_OTHER
  84. }
  85. public static windowAction mWindowAction;
  86. @Override
  87. public void showCommonValue(CommonValueResponse commonValueResponse, String key) {
  88. if (key.equals(Constants.KEY_MAX_FRIEND_NUM)) {
  89. enableFriendsCounts = Integer.parseInt(commonValueResponse.getMsg());
  90. LogUtils.e(TAG + " 当前可添加最大好友数为 : " + enableFriendsCounts);
  91. }
  92. }
  93. @Override
  94. public void showAllowedArea(CommonValueResponse commonValueResponse) {
  95. allowedArea = commonValueResponse.getMsg();
  96. }
  97. @Override
  98. public void showEmpty() {
  99. }
  100. @Override
  101. public void showError(String message) {
  102. }
  103. @Override
  104. protected void onServiceConnected() {
  105. super.onServiceConnected();
  106. LogUtils.d(TAG + " onServiceConnected");
  107. mGetMoney = new getMoneyFunction(this);
  108. mHandleChat = new handleChatFunction(this);
  109. mAddFriendFunction = new addFriendFunction(this);
  110. mAddGroupFunction = new addGroupFunction(this);
  111. mAddAutoReplyFunction = new addAutoReplyFunction(this);
  112. mGetCommonValuePresenter = new getCommonValuePresenter(this);
  113. mGetCommonValuePresenter.getAllowedArea();
  114. pref = getSharedPreferences("robotData", Context.MODE_PRIVATE);
  115. mAutoReplyService = this;
  116. if (pref.getString(savedRobotId, null) != null) {
  117. mGetCommonValuePresenter.getValueByKey(pref.getString(savedRobotId, null), Constants.KEY_MAX_FRIEND_NUM);
  118. }
  119. Subscription subscription = RxBus.toObservable(eventClassFunction.class)
  120. .subscribe(new Action1<eventClassFunction>() {
  121. @Override
  122. public void call(eventClassFunction eventClassFunction) {
  123. if (eventClassFunction.isNeedToWork()) {
  124. doTheUndoWork();
  125. }
  126. }
  127. });
  128. //msgList.add("asd");
  129. mSubscription.add(subscription);
  130. updatePackageInfo();
  131. // App.getRefWatcher(this).watch(this);
  132. }
  133. /**
  134. * 必须重写的方法,响应各种事件,
  135. * 未获得机器人名之前,所有操作首先应获得机器人名 机器人聊天头像为lx
  136. * 查询股票/收钱,建立在加了好友的基础上,故在加好友方法内,添加是否获得机器人名的判断,
  137. * 若没有名字,则先发送“请稍等”,然后点开自己的头像来获得机器人名和微信号
  138. *
  139. * @param event
  140. */
  141. @Override
  142. public void onAccessibilityEvent(final AccessibilityEvent event) {
  143. // LogUtil.e(TAG, "onAccessibilityEvent: " + mEvent.getClassName());
  144. int eventType = event.getEventType();
  145. switch (eventType) {
  146. case AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED:// 通知栏事件,仅有在锁屏状态下, 或者亮屏后台状态下才会发送通知
  147. LogUtils.d("maptrix : get notification event");
  148. if (pref.getString(savedRobotId, null) != null) {
  149. mGetCommonValuePresenter.getValueByKey(pref.getString(savedRobotId, null), Constants.KEY_MAX_FRIEND_NUM);
  150. }
  151. List<CharSequence> texts = event.getText();
  152. if (!texts.isEmpty()) {
  153. for (CharSequence text : texts) {
  154. LogUtils.d("maptrix : onAccessibilityEvent: texts size is " + texts.size());
  155. String content = text.toString();
  156. LogUtils.d("maptrix : onAccessibilityEvent: content is " + content);
  157. if (!TextUtils.isEmpty(content)) {
  158. if (!isScreenOn()) {
  159. lightOnScreen();
  160. LogUtils.d("maptrix : the screen is black");
  161. sendNotifacationReply(event);
  162. } else {
  163. LogUtils.d("maptrix : the screen is on");
  164. sendNotifacationReply(event);
  165. }
  166. }
  167. }
  168. }
  169. break;
  170. case AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED:
  171. LogUtils.d("maptrix : window_stat_changed,event name:" + event.getClassName());
  172. LogUtils.d(TAG + " onAccessibilityEvent: " + mActionType);
  173. if (event.getClassName().equals("com.tencent.mm.ui.base.p")) {
  174. /* if (isSendingPermissionRequest) {
  175. AutoReplyService.isInSeeRoomInfoUICanNotGoBack = false;
  176. } else {
  177. AutoReplyService.isInSeeRoomInfoUICanNotGoBack = true;
  178. }
  179. isSendingPermissionRequest = false;*/
  180. break;
  181. } else if (event.getClassName().equals("com.tencent.mm.plugin.profile.ui.ContactInfoUI")) {
  182. isSendingPermissionRequest = false;
  183. } else if (event.getClassName().equals("com.tencent.mm.ui.widget.a.c")) {
  184. if (mActionType == actionType.isGroupInvited) {
  185. //如果出现因为隐私原因拒绝添加好友的窗口,那么需要设置isFriendSend标志位为true,
  186. //当再次出现ContactUIINfo界面后,直接返回。
  187. LogUtils.d("TYPE_WINDOW_STATE_CHANGED,window:com.tencent.mm.ui.widget.a.c,setFriendSend = true");
  188. // mAddGroupFunction.setFriendSend(true);
  189. }
  190. }
  191. if (!hasAction) {
  192. LogUtils.d(TAG + " hasAction = false");
  193. break;
  194. }
  195. // mActionType = actionType.isGroupInvited;
  196. //mAddGroupFunction.setHasGetMMid(true);
  197. // mAddGroupFunction.addGroup(event);;
  198. judgeAction(event);
  199. break;
  200. case AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED:
  201. LogUtils.d("maptrix : window content changed " + event.getClassName() + inMineView + msgView); //window content内容变化时候调用
  202. if (inMineView && msgView) {
  203. inMineView = false;
  204. msgView = false;
  205. changeStateEvent.setEventType(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
  206. changeStateEvent.setClassName("com.tencent.mm.ui.LauncherUI");
  207. SystemClock.sleep(500);
  208. onAccessibilityEvent(changeStateEvent);
  209. //SystemClock.sleep(500);
  210. }
  211. // if (mActionType == actionType.isGroupInvited && event.getClassName().equals("android.webkit.WebView") && !mAddGroupFunction.webViewFlag) {
  212. // SystemClock.sleep(2000);//用于界面更新
  213. // webViewEvent.setEventType(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
  214. // webViewEvent.setClassName("com.tencent.mm.plugin.webview.ui.tools.WebViewUI");
  215. // mAddGroupFunction.setWebViewFlag(true);
  216. // onAccessibilityEvent(webViewEvent);
  217. // }
  218. break;
  219. }
  220. }
  221. /**
  222. * 对事件进行判断
  223. */
  224. private void judgeAction(AccessibilityEvent event) {
  225. switch (mActionType) {
  226. case isChat:
  227. mHandleChat.setSenderName(msgSenderName);
  228. mHandleChat.handleChat(event, scontent);
  229. break;
  230. case isAddFriend:
  231. mAddFriendFunction.addFriend(event);
  232. break;
  233. case isHongBao:
  234. mGetMoney.setActionType(actionType.isHongBao);
  235. mGetMoney.openMoney(event);
  236. break;
  237. case isZhuanZhang:
  238. mGetMoney.setActionType(actionType.isZhuanZhang);
  239. mGetMoney.openMoney(event);
  240. break;
  241. case isGroupInvited:
  242. mAddGroupFunction.addGroup(event);
  243. break;
  244. case isAddAutoReply:
  245. mAddAutoReplyFunction.addAutoReply(event);
  246. break;
  247. default:
  248. break;
  249. }
  250. }
  251. /**
  252. * 消息冲突处理
  253. */
  254. private void doTheUndoWork() {
  255. /**
  256. * 解决思路
  257. * 当在动作进行中来消息了,就把消息的发送人和内容存起来,在动作结束后统一回到聊天页面,判断事件
  258. * 此处自行判断小红点然后处理事件
  259. */
  260. LogUtils.d(TAG + " doTheUndoWork: ");
  261. //遍历聊天列表,找没做的任务,右上角有数字的
  262. //coversationWithAppBrandListView(cwp)-->LinearLayout(b4m)-->...
  263. // 每一条名称部分的ID为b4o,名称下的消息缩略内容为b4q,头像右上角的带数字小红点为textview,Id为mm
  264. SystemClock.sleep(3000);
  265. AccessibilityNodeInfo root = getRootInActiveWindow();
  266. AccessibilityNodeInfo msgLists = AccessibilityHelper.findNodeInfosById(root, "com.tencent.mm:id/cwp");
  267. if (msgLists == null) {
  268. LogUtils.e(TAG + " doTheUndoWork: msg list size is null ");
  269. return;
  270. }
  271. List<AccessibilityNodeInfo> msg = msgLists.findAccessibilityNodeInfosByViewId("com.tencent.mm:id/b4m");
  272. LogUtils.e(TAG + " doTheUndoWork: " + msg.size());
  273. if (msg.size() == 0) {
  274. return;
  275. }
  276. for (int i = 0; i < msg.size(); i++) {
  277. AccessibilityNodeInfo perMsg = msg.get(i);
  278. AccessibilityNodeInfo redPoint = AccessibilityHelper.findNodeInfosById(perMsg, "com.tencent.mm:id/mm");
  279. if (redPoint == null) {
  280. LogUtils.e(TAG + " doTheUndoWork: the" + perMsg.getContentDescription() + " mm is null");
  281. continue;
  282. }
  283. int theMsgCount = Integer.parseInt(redPoint.getText().toString());
  284. LogUtils.e(TAG + " doTheUndoWork: " + theMsgCount);
  285. if (theMsgCount > 0) {
  286. AccessibilityNodeInfo underTextNode = AccessibilityHelper.findNodeInfosById(perMsg, "com.tencent.mm:id/b4q");
  287. AccessibilityNodeInfo theMsgName = AccessibilityHelper.findNodeInfosById(perMsg, "com.tencent.mm:id/b4o");
  288. if (underTextNode == null) {
  289. LogUtils.e(TAG + " doTheUndoWork: underTextNode is null");
  290. continue;
  291. }
  292. if (theMsgName == null) {
  293. LogUtils.e(TAG + " doTheUndoWork: the msgname is null");
  294. continue;
  295. }
  296. if (theMsgName.getText().toString().equals("腾讯新闻") || theMsgName.getText().toString().equals("服务通知")) {
  297. continue;
  298. }
  299. CharSequence underText = underTextNode.getText();
  300. CharSequence msgName = theMsgName.getText();
  301. judgeText(underText, msgName);
  302. doInBackEvent.setClassName("com.tencent.mm.ui.LauncherUI");
  303. AccessibilityHelper.performClick(perMsg);
  304. judgeAction(doInBackEvent);
  305. return;
  306. }
  307. }
  308. //在这判断是否有未加的好友
  309. //LauncherUIBottomTabView(bn)-->LinearLayout(child 0,无id)-->RelativeLayout(通讯录框框的布局,没有id,child 1,可点击)
  310. // -->TextView(d3s) 记录了通讯录未加好友数目
  311. if (hasUnAddFriend() && enableFriendsCounts != 0) {
  312. addUnaddFriend();
  313. return;
  314. }
  315. AccessibilityNodeInfo underTab = AccessibilityHelper.findNodeInfosById(root, "com.tencent.mm:id/bn");
  316. if (underTab == null) {
  317. LogUtils.e(TAG + " doTheUndoWork: under tab is null");
  318. performGlobalAction(GLOBAL_ACTION_HOME);
  319. return;
  320. }
  321. AccessibilityNodeInfo mineNode = underTab.getChild(0).getChild(3);
  322. if (mineNode == null) {
  323. LogUtils.e(TAG + " doTheUndoWork: mine node is null");
  324. performGlobalAction(GLOBAL_ACTION_HOME);
  325. return;
  326. }
  327. AccessibilityHelper.performClick(mineNode);
  328. inMineView = true;
  329. mActionType = actionType.isNone;
  330. hasAction = false;
  331. msgList.clear();
  332. LogUtils.e(TAG + " doTheUndoWork: " + inMineView);
  333. //performGlobalAction(GLOBAL_ACTION_HOME);
  334. }
  335. /**
  336. * 判断是否有没加的好友
  337. */
  338. private boolean hasUnAddFriend() {
  339. AccessibilityNodeInfo root = getRootInActiveWindow();
  340. AccessibilityNodeInfo underTabNode = AccessibilityHelper.findNodeInfosById(root, "com.tencent.mm:id/bn");
  341. if (underTabNode == null) {
  342. LogUtils.e(TAG + " doTheUndoWork: underTab is null");
  343. return false;
  344. }
  345. AccessibilityNodeInfo contractTabNode = underTabNode.getChild(0).getChild(1);
  346. if (contractTabNode == null) {
  347. LogUtils.e(TAG + " doTheUndoWork: contractNode is null");
  348. return false;
  349. }
  350. AccessibilityNodeInfo unAddFriendCountNode = AccessibilityHelper.findNodeInfosById(contractTabNode, "com.tencent.mm:id/d3s");
  351. if (unAddFriendCountNode == null) {
  352. LogUtils.e(TAG + " addUnaddFriend: unAddCountNode is null");
  353. return false;
  354. }
  355. CharSequence count = unAddFriendCountNode.getText();
  356. if (count == null) {
  357. return false;
  358. }
  359. String strCount = count.toString();
  360. AccessibilityHelper.performClick(contractTabNode);
  361. return true;
  362. }
  363. /**
  364. * 添加没加的好友
  365. */
  366. private void addUnaddFriend() {
  367. AccessibilityNodeInfo root = getRootInActiveWindow();
  368. mActionType = actionType.isAddFriend;
  369. //通讯录界面,整体是个list(m_),第一个子节点是朋友推荐list,包含未加好友那一行的布局id为brc
  370. AccessibilityNodeInfo listNode = AccessibilityHelper.findNodeInfosById(root, "com.tencent.mm:id/m_");
  371. if (listNode == null) {
  372. LogUtils.e(TAG + " addUnaddFriend: tab view list node is null");
  373. return;
  374. }
  375. AccessibilityNodeInfo unAddFriendNode = AccessibilityHelper.findNodeInfosById(listNode, "com.tencent.mm:id/brc");
  376. if (unAddFriendNode == null) {
  377. LogUtils.e(TAG + " addUnaddFriend: unaddFriend node is null");
  378. return;
  379. }
  380. AccessibilityEvent event2 = AccessibilityEvent.obtain();
  381. event2.setClassName("com.tencent.mm.plugin.subapp.ui.friend.FMessageConversationUI");
  382. AccessibilityHelper.performClick(unAddFriendNode);
  383. }
  384. /**
  385. * 在聊天列表通过聊天名称下的简略消息来判断消息内容
  386. */
  387. private void judgeText(CharSequence charSequence, CharSequence msgName) {
  388. if (charSequence == null) {
  389. mActionType = actionType.isChat;
  390. scontent = null;
  391. msgSenderName = msgName.toString();
  392. return;
  393. }
  394. scontent = charSequence.toString();
  395. msgSenderName = msgName.toString();
  396. String str = charSequence.toString();
  397. judgeMsgText(str);
  398. }
  399. private void findallchild(AccessibilityNodeInfo root) {
  400. if (root.getChildCount() != 0) {
  401. int size = root.getChildCount();
  402. for (int i = 0; i < size; i++) {
  403. AccessibilityNodeInfo child = root.getChild(i);
  404. LogUtils.d(TAG + " findallchild: " + child.getContentDescription() + " " + child.getClassName() + " " + child.getViewIdResourceName() + " " + child.getText());
  405. findallchild(child);
  406. }
  407. }
  408. }
  409. /**
  410. * 消息栏处理
  411. *
  412. * @param event TODO:找到获得消息标题的办法
  413. */
  414. private void sendNotifacationReply(AccessibilityEvent event) {
  415. //正常的弹框通知
  416. if (event.getParcelableData() != null
  417. && event.getParcelableData() instanceof Notification) {
  418. hasAction = true;
  419. if (isInAction) {
  420. //暂时无法获取到消息标题用作判断,将所有收到的消息都存下来,遍历列表时候能找到就找到找不到无所谓
  421. LogUtils.e(Constants.TAG_PREFIX + TAG + "sendNotifacationReply: in action");
  422. msgList.add(msgSenderName.length() + msgSenderName + mActionType);
  423. return;
  424. }
  425. Notification notification = (Notification) event
  426. .getParcelableData();
  427. String content = notification.tickerText.toString();
  428. if (content.contains(":")) {
  429. final String[] cc = content.split(":");
  430. msgSenderName = cc[0].trim();//发送者名字,非消息标题
  431. scontent = cc[1].trim();//发送内容
  432. } else {
  433. final String[] cc = content.split("请求添加");//加好友的时候会调用这边,基本没啥用
  434. msgSenderName = cc[0].trim();//发送者名字,非消息标题
  435. scontent = "请求添加你为朋友";
  436. }
  437. LogUtils.i("maptrix : sender name =" + msgSenderName);
  438. LogUtils.i("maptrix : sender content =" + scontent);
  439. judgeMsgText(scontent);
  440. if (inMineView) {
  441. msgView = true;
  442. }
  443. PendingIntent pendingIntent = notification.contentIntent;
  444. try {
  445. pendingIntent.send(); //执行点击
  446. LogUtils.e(TAG + " sendNotifacationReply: dowwwwwwnnnnnn");
  447. } catch (PendingIntent.CanceledException e) {
  448. e.printStackTrace();
  449. }
  450. }
  451. //Toast通知不走上面的if,下代码出现在加好友过频情况
  452. if (event.getText().get(0).toString().contains("操作过于频繁")) {
  453. //addFriendError = true;
  454. mHandleChat.setAddFriendError(true);
  455. mAddGroupFunction.setAddFriendError(true);
  456. }
  457. if (event.getText().get(0).toString().contains("已发送")) {
  458. //isFriendSend = true;
  459. mHandleChat.setFriendSend(true);
  460. mAddGroupFunction.setFriendSend(true);
  461. }
  462. if (event.getText().get(0).toString().contains("添加联系人失败")) {
  463. //do someting
  464. mHandleChat.setFriendSend(true);
  465. AccessibilityHelper.performBack(mAutoReplyService);
  466. }
  467. }
  468. /**
  469. * 判断消息是否包含特定的字符串
  470. */
  471. private void judgeMsgText(String content) {
  472. LogUtils.e(TAG + " judgeMsgText: " + content);
  473. if (content.contains("[微信红包]")) {
  474. mActionType = actionType.isHongBao;
  475. } else if (content.contains("[转账]")) {
  476. mActionType = actionType.isZhuanZhang;
  477. } else if (content.contains("请求添加你为朋友")) {
  478. if (enableFriendsCounts == 0) {
  479. return;
  480. }
  481. mActionType = actionType.isAddFriend;
  482. ;
  483. } else if (content.contains("邀请你加入群聊")) {
  484. if (enableFriendsCounts == 0) {
  485. return;
  486. }
  487. mActionType = actionType.isGroupInvited;
  488. } else if (content.contains("我通过了你的朋友验证请求")) {//由于长度的限制,不能留太长
  489. mActionType = actionType.isAddAutoReply;
  490. } else {
  491. mActionType = actionType.isChat;
  492. }
  493. mWindowAction = windowAction.WINDOW_LAUNCHER;
  494. }
  495. //基本无效的代码,无法判断微信是否在前台
  496. /**
  497. * 判断指定的应用是否在前台运行
  498. *
  499. * @param packageName
  500. * @return
  501. */
  502. private boolean isAppForeground(String packageName) {
  503. ActivityManager am = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
  504. ComponentName cn = am.getRunningTasks(1).get(0).topActivity;
  505. String currentPackageName = cn.getPackageName();
  506. LogUtils.d("maptrix : isAppForeground: " + currentPackageName);
  507. if (!TextUtils.isEmpty(currentPackageName) && currentPackageName.equals(packageName)) {
  508. return true;
  509. }
  510. return false;
  511. }
  512. /**
  513. * 将当前应用运行到前台
  514. */
  515. private void bring2Front() {
  516. ActivityManager activtyManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
  517. List<ActivityManager.RunningTaskInfo> runningTaskInfos = activtyManager.getRunningTasks(3);
  518. for (ActivityManager.RunningTaskInfo runningTaskInfo : runningTaskInfos) {
  519. if (this.getPackageName().equals(runningTaskInfo.topActivity.getPackageName())) {
  520. activtyManager.moveTaskToFront(runningTaskInfo.id, ActivityManager.MOVE_TASK_WITH_HOME);
  521. return;
  522. }
  523. }
  524. }
  525. /**
  526. * 回到系统桌面
  527. */
  528. private void back2Home() {
  529. mWindowAction = windowAction.WINDOW_NONE;
  530. hasAction = false;
  531. mActionType = actionType.isNone;
  532. isGroupChat = false;
  533. Intent home = new Intent(Intent.ACTION_MAIN);
  534. home.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  535. home.addCategory(Intent.CATEGORY_HOME);
  536. startActivity(home);
  537. }
  538. /**
  539. * 判断系统是否为黑屏
  540. */
  541. private boolean isScreenOn() {
  542. LogUtils.e(TAG + " isScreenOn: ");
  543. PowerManager powerManager = (PowerManager) getSystemService(Context.POWER_SERVICE);
  544. return powerManager.isScreenOn();
  545. }
  546. /**
  547. * 点亮屏幕
  548. */
  549. private void lightOnScreen() {
  550. //获取电源管理器对象
  551. PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
  552. //获取PowerManager.WakeLock对象,后面的参数|表示同时传入两个值,最后的是调试用的Tag
  553. PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.ACQUIRE_CAUSES_WAKEUP | PowerManager.SCREEN_BRIGHT_WAKE_LOCK, "MarketingRobot");
  554. //点亮屏幕
  555. wl.acquire();
  556. }
  557. /**
  558. * 系统是否在锁屏状态
  559. *
  560. * @return
  561. */
  562. private boolean isScreenLocked() {
  563. KeyguardManager keyguardManager = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);
  564. return keyguardManager.inKeyguardRestrictedInputMode();
  565. }
  566. private void wakeAndUnlock() {
  567. LogUtils.e(TAG + " wakeAndUnlock: ");
  568. //获取电源管理器对象
  569. PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
  570. //获取PowerManager.WakeLock对象,后面的参数|表示同时传入两个值,最后的是调试用的Tag
  571. PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.ACQUIRE_CAUSES_WAKEUP | PowerManager.SCREEN_BRIGHT_WAKE_LOCK, "MarketingRobot");
  572. //点亮屏幕
  573. wl.acquire();
  574. //得到键盘锁管理器对象
  575. KeyguardManager km = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);
  576. KeyguardManager.KeyguardLock kl = km.newKeyguardLock("unLock");
  577. //解锁
  578. kl.disableKeyguard();
  579. }
  580. /**
  581. * 判断当前服务是否正在运行
  582. */
  583. @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
  584. public static boolean isRunning() {
  585. if (mAutoReplyService == null) {
  586. return false;
  587. }
  588. AccessibilityManager accessibilityManager = (AccessibilityManager) mAutoReplyService.getSystemService(Context.ACCESSIBILITY_SERVICE);
  589. AccessibilityServiceInfo info = mAutoReplyService.getServiceInfo();
  590. if (info == null) {
  591. return false;
  592. }
  593. List<AccessibilityServiceInfo> list = accessibilityManager.getEnabledAccessibilityServiceList(AccessibilityServiceInfo.FEEDBACK_GENERIC);
  594. Iterator<AccessibilityServiceInfo> iterator = list.iterator();
  595. boolean isConnect = false;
  596. while (iterator.hasNext()) {
  597. AccessibilityServiceInfo i = iterator.next();
  598. if (i.getId().equals(info.getId())) {
  599. isConnect = true;
  600. break;
  601. }
  602. }
  603. if (!isConnect) {
  604. return false;
  605. }
  606. return true;
  607. }
  608. /**
  609. * 获取微信的版本
  610. */
  611. public static int getWechatVersion() {
  612. if (mWechatPackageInfo == null) {
  613. return 0;
  614. }
  615. return mWechatPackageInfo.versionCode;
  616. }
  617. /**
  618. * 更新微信包信息
  619. */
  620. private void updatePackageInfo() {
  621. try {
  622. mWechatPackageInfo = getPackageManager().getPackageInfo(MM_PNAME, 0);
  623. } catch (PackageManager.NameNotFoundException e) {
  624. e.printStackTrace();
  625. }
  626. }
  627. @Override
  628. public void onInterrupt() {
  629. }
  630. }