HongbaoSignature.java 3.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. package me.yoqi.app.wxredpacket.utils;
  2. import android.graphics.Rect;
  3. import android.view.accessibility.AccessibilityNodeInfo;
  4. /**
  5. * Created by Zhongyi on 1/21/16.
  6. */
  7. public class HongbaoSignature {
  8. public String sender, content, time, contentDescription = "", commentString;
  9. public boolean others;
  10. public boolean generateSignature(AccessibilityNodeInfo node, String excludeWords) {
  11. try {
  12. /* The hongbao container node. It should be a LinearLayout. By specifying that, we can avoid text messages. */
  13. AccessibilityNodeInfo hongbaoNode = node.getParent();
  14. if (!"android.widget.LinearLayout".equals(hongbaoNode.getClassName())) return false;
  15. /* The text in the hongbao. Should mean something. */
  16. String hongbaoContent = hongbaoNode.getChild(0).getText().toString();
  17. if (hongbaoContent == null || "查看红包".equals(hongbaoContent)) return false;
  18. /* Check the user's exclude words list. */
  19. String[] excludeWordsArray = excludeWords.split(" +");
  20. for (String word : excludeWordsArray) {
  21. if (word.length() > 0 && hongbaoContent.contains(word)) return false;
  22. }
  23. /* The container node for a piece of message. It should be inside the screen.
  24. Or sometimes it will get opened twice while scrolling. */
  25. AccessibilityNodeInfo messageNode = hongbaoNode.getParent();
  26. Rect bounds = new Rect();
  27. messageNode.getBoundsInScreen(bounds);
  28. if (bounds.top < 0) return false;
  29. /* The sender and possible timestamp. Should mean something too. */
  30. String[] hongbaoInfo = getSenderContentDescriptionFromNode(messageNode);
  31. if (this.getSignature(hongbaoInfo[0], hongbaoContent, hongbaoInfo[1]).equals(this.toString())) return false;
  32. /* So far we make sure it's a valid new coming hongbao. */
  33. this.sender = hongbaoInfo[0];
  34. this.time = hongbaoInfo[1];
  35. this.content = hongbaoContent;
  36. return true;
  37. } catch (Exception e) {
  38. e.printStackTrace();
  39. return false;
  40. }
  41. }
  42. @Override
  43. public String toString() {
  44. return this.getSignature(this.sender, this.content, this.time);
  45. }
  46. private String getSignature(String... strings) {
  47. String signature = "";
  48. for (String str : strings) {
  49. if (str == null) return null;
  50. signature += str + "|";
  51. }
  52. return signature.substring(0, signature.length() - 1);
  53. }
  54. public String getContentDescription() {
  55. return this.contentDescription;
  56. }
  57. public void setContentDescription(String description) {
  58. this.contentDescription = description;
  59. }
  60. private String[] getSenderContentDescriptionFromNode(AccessibilityNodeInfo node) {
  61. int count = node.getChildCount();
  62. String[] result = {"unknownSender", "unknownTime"};
  63. for (int i = 0; i < count; i++) {
  64. AccessibilityNodeInfo thisNode = node.getChild(i);
  65. if ("android.widget.ImageView".equals(thisNode.getClassName()) && "unknownSender".equals(result[0])) {
  66. CharSequence contentDescription = thisNode.getContentDescription();
  67. if (contentDescription != null) result[0] = contentDescription.toString().replaceAll("头像$", "");
  68. } else if ("android.widget.TextView".equals(thisNode.getClassName()) && "unknownTime".equals(result[1])) {
  69. CharSequence thisNodeText = thisNode.getText();
  70. if (thisNodeText != null) result[1] = thisNodeText.toString();
  71. }
  72. }
  73. return result;
  74. }
  75. public void cleanSignature() {
  76. this.content = "";
  77. this.time = "";
  78. this.sender = "";
  79. }
  80. }