2
2
3
3
import android .annotation .SuppressLint ;
4
4
import android .content .Context ;
5
+ import android .support .annotation .NonNull ;
5
6
6
7
import org .w3c .dom .Element ;
7
8
import org .w3c .dom .Node ;
8
9
import org .w3c .dom .NodeList ;
9
10
11
+ import java .util .ArrayList ;
12
+ import java .util .List ;
13
+
10
14
import javax .annotation .Nullable ;
11
15
12
16
import fr .free .nrw .commons .BuildConfig ;
13
17
import fr .free .nrw .commons .R ;
14
18
19
+ import static fr .free .nrw .commons .notification .NotificationType .THANK_YOU_EDIT ;
20
+ import static fr .free .nrw .commons .notification .NotificationType .UNKNOWN ;
21
+
15
22
public class NotificationUtils {
16
23
17
24
private static final String COMMONS_WIKI = "commonswiki" ;
@@ -30,6 +37,56 @@ public static NotificationType getNotificationType(Node document) {
30
37
return NotificationType .handledValueOf (type );
31
38
}
32
39
40
+ public static List <Notification > getNotificationsFromBundle (Context context , Node document ) {
41
+ Element bundledNotifications = getBundledNotifications (document );
42
+ NodeList childNodes = bundledNotifications .getChildNodes ();
43
+
44
+ List <Notification > notifications = new ArrayList <>();
45
+ for (int i = 0 ; i < childNodes .getLength (); i ++) {
46
+ Node node = childNodes .item (i );
47
+ if (isUsefulNotification (node )) {
48
+ notifications .add (getNotificationFromApiResult (context , node ));
49
+ }
50
+ }
51
+ return notifications ;
52
+ }
53
+
54
+ @ NonNull
55
+ public static List <Notification > getNotificationsFromList (Context context , NodeList childNodes ) {
56
+ List <Notification > notifications = new ArrayList <>();
57
+ for (int i = 0 ; i < childNodes .getLength (); i ++) {
58
+ Node node = childNodes .item (i );
59
+ if (isUsefulNotification (node )) {
60
+ if (isBundledNotification (node )) {
61
+ notifications .addAll (getNotificationsFromBundle (context , node ));
62
+ } else {
63
+ notifications .add (getNotificationFromApiResult (context , node ));
64
+ }
65
+ }
66
+ }
67
+
68
+ return notifications ;
69
+ }
70
+
71
+ private static boolean isUsefulNotification (Node node ) {
72
+ return isCommonsNotification (node )
73
+ && !getNotificationType (node ).equals (UNKNOWN )
74
+ && !getNotificationType (node ).equals (THANK_YOU_EDIT );
75
+ }
76
+
77
+ public static boolean isBundledNotification (Node document ) {
78
+ Element bundleElement = getBundledNotifications (document );
79
+ if (bundleElement == null ) {
80
+ return false ;
81
+ }
82
+
83
+ return bundleElement .getChildNodes ().getLength () > 0 ;
84
+ }
85
+
86
+ private static Element getBundledNotifications (Node document ) {
87
+ return (Element ) getNode (document , "bundledNotifications" );
88
+ }
89
+
33
90
public static Notification getNotificationFromApiResult (Context context , Node document ) {
34
91
NotificationType type = getNotificationType (document );
35
92
@@ -43,7 +100,7 @@ public static Notification getNotificationFromApiResult(Context context, Node do
43
100
notificationText = context .getString (R .string .notifications_thank_you_edit );
44
101
break ;
45
102
case EDIT_USER_TALK :
46
- notificationText = getNotificationHeader (document );
103
+ notificationText = getNotificationText (document );
47
104
break ;
48
105
case MENTION :
49
106
notificationText = getMentionMessage (context , document );
@@ -56,6 +113,14 @@ public static Notification getNotificationFromApiResult(Context context, Node do
56
113
return new Notification (type , notificationText , getTimestamp (document ), description , link , iconUrl );
57
114
}
58
115
116
+ private static String getNotificationText (Node document ) {
117
+ String notificationBody = getNotificationBody (document );
118
+ if (notificationBody == null || notificationBody .trim ().equals ("" )) {
119
+ return getNotificationHeader (document );
120
+ }
121
+ return notificationBody ;
122
+ }
123
+
59
124
private static String getNotificationHeader (Node document ) {
60
125
Node body = getNode (getModel (document ), "header" );
61
126
if (body != null ) {
@@ -66,6 +131,16 @@ private static String getNotificationHeader(Node document) {
66
131
}
67
132
}
68
133
134
+ private static String getNotificationBody (Node document ) {
135
+ Node body = getNode (getModel (document ), "body" );
136
+ if (body != null ) {
137
+ String textContent = body .getTextContent ();
138
+ return textContent .replace ("<strong>" , "" ).replace ("</strong>" , "" );
139
+ } else {
140
+ return "" ;
141
+ }
142
+ }
143
+
69
144
private static String getMentionDescription (Node document ) {
70
145
Node body = getNode (getModel (document ), "body" );
71
146
return body != null ? body .getTextContent () : "" ;
0 commit comments