@Test public void testLocalizedSummary() { Resources resourcesMock = Mockito.mock(Resources.class); Mockito.doReturn("\\d+ neue Nachrichten") .when(resourcesMock).getString(R.string.new_messages_summary_pattern); Mockito.doReturn("\\d+ (neue )?Nachrichten (aus|von) \\d+ Chats") .when(resourcesMock).getString(R.string.new_messages_multiple_chats_summary_pattern); MessageExtractor extractor = new GroupSummaryMessageExtractor(resourcesMock, false); assertTitleAndTextEqual("Alice", "Marco", getTitleAndText(extractor, SUMMARY, "Alice", "Marco")); assertTitleAndTextEqual("Bob", null, getTitleAndText(extractor, REGULAR, "Bob", "Polo")); assertTitleAndTextEqual("Bob", "Polo", getTitleAndText(extractor, SUMMARY, "WhatsApp", "2 neue Nachrichten von 2 Chats", "Alice: Marco", "Bob: Polo")); assertTitleAndTextEqual("Bob", null, getTitleAndText(extractor, REGULAR, "Bob", "Polo")); assertTitleAndTextEqual("Bob", "I win!", getTitleAndText(extractor, SUMMARY, "WhatsApp", "3 Nachrichten aus 2 Chats", "Alice: Marco", "Bob: Polo", "Bob: I win!")); }
@Override public CharSequence[] getTitleAndText(String appPackageName, Bundle extras, int notificationFlags) { DebugLog debugLog = getDebugLog(); if (isLoggingEnabled()) { debugLog.writeLog("Entered GroupSummaryMessageExtractor getTitleAndText method."); debugLog.writeLog("NotificationFlags = " + notificationFlags); } CharSequence notificationTitle = extras.getCharSequence(Notification.EXTRA_TITLE); CharSequence notificationText = null; if ((notificationFlags & Notification.FLAG_GROUP_SUMMARY) != 0) { if (isLoggingEnabled()) { debugLog.writeLog("Notification is a group summary. Processing."); } notificationText = extras.getCharSequence(Notification.EXTRA_TEXT); if (notificationText == null && notificationTitle == null) { return null; } if (notificationText == null || !matchesAnyPattern(notificationText, mAllNewMessagesPatterns)) { if (isLoggingEnabled()) { debugLog.writeLog("Regular text. Using generic approach."); } mLastSeenMessageHash = hash(notificationTitle, notificationText); return super.getTitleAndText(appPackageName, extras, notificationFlags); } CharSequence[] lines = extras.getCharSequenceArray(Notification.EXTRA_TEXT_LINES); if (lines == null) { if (isLoggingEnabled()) { debugLog.writeLog("EXTRA text lines is null. Using generic approach."); } mLastSeenMessageHash = hash(notificationTitle, notificationText); return super.getTitleAndText(appPackageName, extras, notificationFlags); } int newestMessageIndex = mNewMessagesFirst ? 0 : lines.length - 1; if (matchesAnyPattern(notificationText, mNewMessagesMultipleChatsPatterns)) { if (isLoggingEnabled()) { debugLog.writeLog("N new messages from M chats style."); } int pos = findFirstNewMessage(lines, null); if (pos < 0) { notificationText = buildMultiMessage(lines, -pos - 1, mNewMessagesFirst ? -1 : 1); } else { notificationText = buildMessage(lines, pos, mNewMessagesFirst ? -1 : 1, true); notificationTitle = getSender(lines[newestMessageIndex]); } mLastSeenMessageHash = hash(lines[newestMessageIndex], 0); } else { if (isLoggingEnabled()) { debugLog.writeLog("N new messages style."); } int pos = findFirstNewMessage(lines, notificationTitle); notificationText = buildMessage(lines, pos, mNewMessagesFirst ? -1 : 1, false); mLastSeenMessageHash = hash(notificationTitle, lines[newestMessageIndex]); } } else { if (isLoggingEnabled()) { debugLog.writeLog("Notification is not a group summary."); } if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT_WATCH) { if (isLoggingEnabled()) { debugLog.writeLog("App is running on Kitkat device. " + "No group summary available on SDK 19. Use generic extractor"); } return super.getTitleAndText(appPackageName, extras, notificationFlags); } } return new CharSequence[] { notificationTitle, notificationText }; }
GroupSummaryMessageExtractor extends BasicMessageExtractor { @Override public CharSequence[] getTitleAndText(String appPackageName, Bundle extras, int notificationFlags) { DebugLog debugLog = getDebugLog(); if (isLoggingEnabled()) { debugLog.writeLog("Entered GroupSummaryMessageExtractor getTitleAndText method."); debugLog.writeLog("NotificationFlags = " + notificationFlags); } CharSequence notificationTitle = extras.getCharSequence(Notification.EXTRA_TITLE); CharSequence notificationText = null; if ((notificationFlags & Notification.FLAG_GROUP_SUMMARY) != 0) { if (isLoggingEnabled()) { debugLog.writeLog("Notification is a group summary. Processing."); } notificationText = extras.getCharSequence(Notification.EXTRA_TEXT); if (notificationText == null && notificationTitle == null) { return null; } if (notificationText == null || !matchesAnyPattern(notificationText, mAllNewMessagesPatterns)) { if (isLoggingEnabled()) { debugLog.writeLog("Regular text. Using generic approach."); } mLastSeenMessageHash = hash(notificationTitle, notificationText); return super.getTitleAndText(appPackageName, extras, notificationFlags); } CharSequence[] lines = extras.getCharSequenceArray(Notification.EXTRA_TEXT_LINES); if (lines == null) { if (isLoggingEnabled()) { debugLog.writeLog("EXTRA text lines is null. Using generic approach."); } mLastSeenMessageHash = hash(notificationTitle, notificationText); return super.getTitleAndText(appPackageName, extras, notificationFlags); } int newestMessageIndex = mNewMessagesFirst ? 0 : lines.length - 1; if (matchesAnyPattern(notificationText, mNewMessagesMultipleChatsPatterns)) { if (isLoggingEnabled()) { debugLog.writeLog("N new messages from M chats style."); } int pos = findFirstNewMessage(lines, null); if (pos < 0) { notificationText = buildMultiMessage(lines, -pos - 1, mNewMessagesFirst ? -1 : 1); } else { notificationText = buildMessage(lines, pos, mNewMessagesFirst ? -1 : 1, true); notificationTitle = getSender(lines[newestMessageIndex]); } mLastSeenMessageHash = hash(lines[newestMessageIndex], 0); } else { if (isLoggingEnabled()) { debugLog.writeLog("N new messages style."); } int pos = findFirstNewMessage(lines, notificationTitle); notificationText = buildMessage(lines, pos, mNewMessagesFirst ? -1 : 1, false); mLastSeenMessageHash = hash(notificationTitle, lines[newestMessageIndex]); } } else { if (isLoggingEnabled()) { debugLog.writeLog("Notification is not a group summary."); } if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT_WATCH) { if (isLoggingEnabled()) { debugLog.writeLog("App is running on Kitkat device. " + "No group summary available on SDK 19. Use generic extractor"); } return super.getTitleAndText(appPackageName, extras, notificationFlags); } } return new CharSequence[] { notificationTitle, notificationText }; } }
GroupSummaryMessageExtractor extends BasicMessageExtractor { @Override public CharSequence[] getTitleAndText(String appPackageName, Bundle extras, int notificationFlags) { DebugLog debugLog = getDebugLog(); if (isLoggingEnabled()) { debugLog.writeLog("Entered GroupSummaryMessageExtractor getTitleAndText method."); debugLog.writeLog("NotificationFlags = " + notificationFlags); } CharSequence notificationTitle = extras.getCharSequence(Notification.EXTRA_TITLE); CharSequence notificationText = null; if ((notificationFlags & Notification.FLAG_GROUP_SUMMARY) != 0) { if (isLoggingEnabled()) { debugLog.writeLog("Notification is a group summary. Processing."); } notificationText = extras.getCharSequence(Notification.EXTRA_TEXT); if (notificationText == null && notificationTitle == null) { return null; } if (notificationText == null || !matchesAnyPattern(notificationText, mAllNewMessagesPatterns)) { if (isLoggingEnabled()) { debugLog.writeLog("Regular text. Using generic approach."); } mLastSeenMessageHash = hash(notificationTitle, notificationText); return super.getTitleAndText(appPackageName, extras, notificationFlags); } CharSequence[] lines = extras.getCharSequenceArray(Notification.EXTRA_TEXT_LINES); if (lines == null) { if (isLoggingEnabled()) { debugLog.writeLog("EXTRA text lines is null. Using generic approach."); } mLastSeenMessageHash = hash(notificationTitle, notificationText); return super.getTitleAndText(appPackageName, extras, notificationFlags); } int newestMessageIndex = mNewMessagesFirst ? 0 : lines.length - 1; if (matchesAnyPattern(notificationText, mNewMessagesMultipleChatsPatterns)) { if (isLoggingEnabled()) { debugLog.writeLog("N new messages from M chats style."); } int pos = findFirstNewMessage(lines, null); if (pos < 0) { notificationText = buildMultiMessage(lines, -pos - 1, mNewMessagesFirst ? -1 : 1); } else { notificationText = buildMessage(lines, pos, mNewMessagesFirst ? -1 : 1, true); notificationTitle = getSender(lines[newestMessageIndex]); } mLastSeenMessageHash = hash(lines[newestMessageIndex], 0); } else { if (isLoggingEnabled()) { debugLog.writeLog("N new messages style."); } int pos = findFirstNewMessage(lines, notificationTitle); notificationText = buildMessage(lines, pos, mNewMessagesFirst ? -1 : 1, false); mLastSeenMessageHash = hash(notificationTitle, lines[newestMessageIndex]); } } else { if (isLoggingEnabled()) { debugLog.writeLog("Notification is not a group summary."); } if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT_WATCH) { if (isLoggingEnabled()) { debugLog.writeLog("App is running on Kitkat device. " + "No group summary available on SDK 19. Use generic extractor"); } return super.getTitleAndText(appPackageName, extras, notificationFlags); } } return new CharSequence[] { notificationTitle, notificationText }; } GroupSummaryMessageExtractor(boolean newMessagesFirst);  GroupSummaryMessageExtractor(Resources res, boolean newMessagesFirst); }
GroupSummaryMessageExtractor extends BasicMessageExtractor { @Override public CharSequence[] getTitleAndText(String appPackageName, Bundle extras, int notificationFlags) { DebugLog debugLog = getDebugLog(); if (isLoggingEnabled()) { debugLog.writeLog("Entered GroupSummaryMessageExtractor getTitleAndText method."); debugLog.writeLog("NotificationFlags = " + notificationFlags); } CharSequence notificationTitle = extras.getCharSequence(Notification.EXTRA_TITLE); CharSequence notificationText = null; if ((notificationFlags & Notification.FLAG_GROUP_SUMMARY) != 0) { if (isLoggingEnabled()) { debugLog.writeLog("Notification is a group summary. Processing."); } notificationText = extras.getCharSequence(Notification.EXTRA_TEXT); if (notificationText == null && notificationTitle == null) { return null; } if (notificationText == null || !matchesAnyPattern(notificationText, mAllNewMessagesPatterns)) { if (isLoggingEnabled()) { debugLog.writeLog("Regular text. Using generic approach."); } mLastSeenMessageHash = hash(notificationTitle, notificationText); return super.getTitleAndText(appPackageName, extras, notificationFlags); } CharSequence[] lines = extras.getCharSequenceArray(Notification.EXTRA_TEXT_LINES); if (lines == null) { if (isLoggingEnabled()) { debugLog.writeLog("EXTRA text lines is null. Using generic approach."); } mLastSeenMessageHash = hash(notificationTitle, notificationText); return super.getTitleAndText(appPackageName, extras, notificationFlags); } int newestMessageIndex = mNewMessagesFirst ? 0 : lines.length - 1; if (matchesAnyPattern(notificationText, mNewMessagesMultipleChatsPatterns)) { if (isLoggingEnabled()) { debugLog.writeLog("N new messages from M chats style."); } int pos = findFirstNewMessage(lines, null); if (pos < 0) { notificationText = buildMultiMessage(lines, -pos - 1, mNewMessagesFirst ? -1 : 1); } else { notificationText = buildMessage(lines, pos, mNewMessagesFirst ? -1 : 1, true); notificationTitle = getSender(lines[newestMessageIndex]); } mLastSeenMessageHash = hash(lines[newestMessageIndex], 0); } else { if (isLoggingEnabled()) { debugLog.writeLog("N new messages style."); } int pos = findFirstNewMessage(lines, notificationTitle); notificationText = buildMessage(lines, pos, mNewMessagesFirst ? -1 : 1, false); mLastSeenMessageHash = hash(notificationTitle, lines[newestMessageIndex]); } } else { if (isLoggingEnabled()) { debugLog.writeLog("Notification is not a group summary."); } if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT_WATCH) { if (isLoggingEnabled()) { debugLog.writeLog("App is running on Kitkat device. " + "No group summary available on SDK 19. Use generic extractor"); } return super.getTitleAndText(appPackageName, extras, notificationFlags); } } return new CharSequence[] { notificationTitle, notificationText }; } GroupSummaryMessageExtractor(boolean newMessagesFirst);  GroupSummaryMessageExtractor(Resources res, boolean newMessagesFirst); @Override CharSequence[] getTitleAndText(String appPackageName, Bundle extras, int notificationFlags); }
GroupSummaryMessageExtractor extends BasicMessageExtractor { @Override public CharSequence[] getTitleAndText(String appPackageName, Bundle extras, int notificationFlags) { DebugLog debugLog = getDebugLog(); if (isLoggingEnabled()) { debugLog.writeLog("Entered GroupSummaryMessageExtractor getTitleAndText method."); debugLog.writeLog("NotificationFlags = " + notificationFlags); } CharSequence notificationTitle = extras.getCharSequence(Notification.EXTRA_TITLE); CharSequence notificationText = null; if ((notificationFlags & Notification.FLAG_GROUP_SUMMARY) != 0) { if (isLoggingEnabled()) { debugLog.writeLog("Notification is a group summary. Processing."); } notificationText = extras.getCharSequence(Notification.EXTRA_TEXT); if (notificationText == null && notificationTitle == null) { return null; } if (notificationText == null || !matchesAnyPattern(notificationText, mAllNewMessagesPatterns)) { if (isLoggingEnabled()) { debugLog.writeLog("Regular text. Using generic approach."); } mLastSeenMessageHash = hash(notificationTitle, notificationText); return super.getTitleAndText(appPackageName, extras, notificationFlags); } CharSequence[] lines = extras.getCharSequenceArray(Notification.EXTRA_TEXT_LINES); if (lines == null) { if (isLoggingEnabled()) { debugLog.writeLog("EXTRA text lines is null. Using generic approach."); } mLastSeenMessageHash = hash(notificationTitle, notificationText); return super.getTitleAndText(appPackageName, extras, notificationFlags); } int newestMessageIndex = mNewMessagesFirst ? 0 : lines.length - 1; if (matchesAnyPattern(notificationText, mNewMessagesMultipleChatsPatterns)) { if (isLoggingEnabled()) { debugLog.writeLog("N new messages from M chats style."); } int pos = findFirstNewMessage(lines, null); if (pos < 0) { notificationText = buildMultiMessage(lines, -pos - 1, mNewMessagesFirst ? -1 : 1); } else { notificationText = buildMessage(lines, pos, mNewMessagesFirst ? -1 : 1, true); notificationTitle = getSender(lines[newestMessageIndex]); } mLastSeenMessageHash = hash(lines[newestMessageIndex], 0); } else { if (isLoggingEnabled()) { debugLog.writeLog("N new messages style."); } int pos = findFirstNewMessage(lines, notificationTitle); notificationText = buildMessage(lines, pos, mNewMessagesFirst ? -1 : 1, false); mLastSeenMessageHash = hash(notificationTitle, lines[newestMessageIndex]); } } else { if (isLoggingEnabled()) { debugLog.writeLog("Notification is not a group summary."); } if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT_WATCH) { if (isLoggingEnabled()) { debugLog.writeLog("App is running on Kitkat device. " + "No group summary available on SDK 19. Use generic extractor"); } return super.getTitleAndText(appPackageName, extras, notificationFlags); } } return new CharSequence[] { notificationTitle, notificationText }; } GroupSummaryMessageExtractor(boolean newMessagesFirst);  GroupSummaryMessageExtractor(Resources res, boolean newMessagesFirst); @Override CharSequence[] getTitleAndText(String appPackageName, Bundle extras, int notificationFlags);  }