Skip to content

Commit 9e5edc4

Browse files
committed
notif [nfc]: Remove unused functions and Class.
These were involved in constructing old notification UI and are no longer needed.
1 parent 792972e commit 9e5edc4

File tree

5 files changed

+8
-226
lines changed

5 files changed

+8
-226
lines changed

android/app/src/main/java/com/zulipmobile/MainApplication.java

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,20 +20,13 @@
2020
import org.unimodules.core.interfaces.SingletonModule;
2121

2222
import com.zulipmobile.generated.BasePackageList;
23-
import com.zulipmobile.notifications.ConversationMap;
2423
import com.zulipmobile.notifications.FCMPushNotifications;
2524
import com.zulipmobile.notifications.NotificationsPackage;
2625
import com.zulipmobile.sharing.SharingPackage;
2726

2827
public class MainApplication extends Application implements ReactApplication {
2928
private final ReactModuleRegistryProvider mModuleRegistryProvider = new ReactModuleRegistryProvider(new BasePackageList().getPackageList(), null);
3029

31-
private ConversationMap conversations;
32-
33-
public ConversationMap getConversations() {
34-
return conversations;
35-
}
36-
3730
private final ReactNativeHost mReactNativeHost = new ReactNativeHost(this) {
3831
@Override
3932
public boolean getUseDeveloperSupport() {
@@ -79,7 +72,6 @@ public void onCreate() {
7972
FCMPushNotifications.createNotificationChannel(this);
8073
SoLoader.init(this, /* native exopackage */ false);
8174
initializeFlipper(this, getReactNativeHost().getReactInstanceManager());
82-
conversations = new ConversationMap();
8375
}
8476

8577
/**

android/app/src/main/java/com/zulipmobile/notifications/FCMPushNotifications.kt

Lines changed: 5 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@ import android.graphics.Color
1313
import android.net.Uri
1414
import android.os.Build
1515
import android.os.Bundle
16-
import android.provider.Settings
17-
import android.text.TextUtils
1816
import android.util.Log
1917
import androidx.core.app.NotificationManagerCompat
2018
import androidx.core.app.NotificationCompat
@@ -56,7 +54,7 @@ private fun logNotificationData(msg: String, data: Bundle) {
5654
Log.v(TAG, "$msg: $data")
5755
}
5856

59-
internal fun onReceived(context: Context, conversations: ConversationMap, mapData: Map<String, String>) {
57+
internal fun onReceived(context: Context, mapData: Map<String, String>) {
6058
// TODO refactor to not need this; reflects a juxtaposition of FCM with old GCM interfaces.
6159
val data = Bundle()
6260
for ((key, value) in mapData) {
@@ -73,14 +71,8 @@ internal fun onReceived(context: Context, conversations: ConversationMap, mapDat
7371
}
7472

7573
if (fcmMessage is MessageFcmMessage) {
76-
addConversationToMap(fcmMessage, conversations)
77-
updateNotification(context, conversations, fcmMessage)
78-
} else if (fcmMessage is RemoveFcmMessage) {
79-
removeMessagesFromMap(conversations, fcmMessage)
80-
if (conversations.isEmpty()) {
81-
NotificationManagerCompat.from(context).cancelAll()
82-
}
83-
}
74+
updateNotification(context, fcmMessage)
75+
} // TODO handle case for RemoveFcmMessage
8476
}
8577

8678
private fun createViewPendingIntent(fcmMessage: MessageFcmMessage, context: Context): PendingIntent {
@@ -157,7 +149,7 @@ private fun createSummaryNotification(
157149
}
158150

159151
private fun updateNotification(
160-
context: Context, conversations: ConversationMap, fcmMessage: MessageFcmMessage) {
152+
context: Context, fcmMessage: MessageFcmMessage) {
161153
val user = Person.Builder().setName("You").build()
162154
val sender = Person.Builder()
163155
.setName(fcmMessage.sender.fullName)
@@ -222,95 +214,13 @@ private fun updateNotification(
222214
}
223215
}
224216

225-
private fun getNotificationSoundUri(context: Context): Uri {
226-
// Note: Provide default notification sound until we found a good sound
227-
// return Uri.parse("${ContentResolver.SCHEME_ANDROID_RESOURCE}://${context.packageName}/${R.raw.zulip}")
228-
return Settings.System.DEFAULT_NOTIFICATION_URI
229-
}
230-
231-
private fun getNotificationBuilder(
232-
context: Context, conversations: ConversationMap, fcmMessage: MessageFcmMessage): NotificationCompat.Builder {
233-
val builder = NotificationCompat.Builder(context, CHANNEL_ID)
234-
235-
val viewPendingIntent = createViewPendingIntent(fcmMessage, context)
236-
builder.setContentIntent(viewPendingIntent)
237-
builder.setAutoCancel(true)
238-
239-
val totalMessagesCount = extractTotalMessagesCount(conversations)
240-
241-
if (BuildConfig.DEBUG) {
242-
builder.setSmallIcon(R.mipmap.ic_launcher)
243-
} else {
244-
builder.setSmallIcon(R.drawable.zulip_notification)
245-
}
246-
247-
// This should agree with `BRAND_COLOR` in the JS code.
248-
builder.setColor(Color.rgb(100, 146, 254))
249-
250-
val nameList = extractNames(conversations)
251-
252-
if (conversations.size == 1 && nameList.size == 1) {
253-
//Only one 1 notification therefore no using of big view styles
254-
if (totalMessagesCount > 1) {
255-
builder.setContentTitle("${fcmMessage.sender.fullName} ($totalMessagesCount)")
256-
} else {
257-
builder.setContentTitle(fcmMessage.sender.fullName)
258-
}
259-
builder.setContentText(fcmMessage.content)
260-
if (fcmMessage.recipient is Recipient.Stream) {
261-
val (stream, topic) = fcmMessage.recipient
262-
val displayTopic = "$stream > $topic"
263-
builder.setSubText("Message on $displayTopic")
264-
}
265-
fetchBitmap(sizedURL(context, fcmMessage.sender.avatarURL, 64f))
266-
?.let { builder.setLargeIcon(it) }
267-
builder.setStyle(NotificationCompat.BigTextStyle().bigText(fcmMessage.content))
268-
} else {
269-
val numConversations = context.resources.getQuantityString(
270-
R.plurals.numConversations, conversations.size, conversations.size)
271-
builder.setContentTitle("$totalMessagesCount messages in $numConversations")
272-
builder.setContentText("Messages from ${TextUtils.join(",", nameList)}")
273-
val inboxStyle = NotificationCompat.InboxStyle(builder)
274-
inboxStyle.setSummaryText(numConversations)
275-
buildNotificationContent(conversations, inboxStyle)
276-
builder.setStyle(inboxStyle)
277-
}
278-
279-
try {
280-
ShortcutBadger.applyCount(context, totalMessagesCount)
281-
} catch (e: Exception) {
282-
Log.e(TAG, "BADGE ERROR: $e")
283-
}
284-
285-
builder.setWhen(fcmMessage.timeMs)
286-
builder.setShowWhen(true)
287-
288-
val vPattern = longArrayOf(0, 100, 200, 100)
289-
// NB the DEFAULT_VIBRATE flag below causes this to have no effect.
290-
// TODO: choose a vibration pattern we like, and unset DEFAULT_VIBRATE.
291-
builder.setVibrate(vPattern)
292-
293-
builder.setDefaults(NotificationCompat.DEFAULT_VIBRATE or NotificationCompat.DEFAULT_LIGHTS)
294-
295-
builder.addAction(createDismissAction(context))
296-
297-
val soundUri = getNotificationSoundUri(context)
298-
builder.setSound(soundUri)
299-
return builder
300-
}
301-
302-
internal fun onOpened(application: ReactApplication, conversations: ConversationMap, data: Bundle) {
217+
internal fun onOpened(application: ReactApplication, data: Bundle) {
303218
logNotificationData("notif opened", data)
304219
notifyReact(application, data)
305-
clearConversations(conversations)
306220
try {
307221
ShortcutBadger.removeCount(application as Context)
308222
} catch (e: Exception) {
309223
Log.e(TAG, "BADGE ERROR: $e")
310224
}
311225

312226
}
313-
314-
internal fun onClear(context: Context, conversations: ConversationMap) {
315-
clearConversations(conversations)
316-
}

android/app/src/main/java/com/zulipmobile/notifications/FcmListenerService.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,7 @@ public void onMessageReceived(RemoteMessage message) {
1515
if (!(applicationContext instanceof MainApplication)) {
1616
return;
1717
}
18-
final ConversationMap conversations =
19-
((MainApplication) applicationContext).getConversations();
20-
FCMPushNotifications.onReceived(this, conversations, message.getData());
18+
FCMPushNotifications.onReceived(this, message.getData());
2119
}
2220

2321
@Override

android/app/src/main/java/com/zulipmobile/notifications/NotificationHelper.kt

Lines changed: 0 additions & 116 deletions
Original file line numberDiff line numberDiff line change
@@ -2,34 +2,16 @@
22

33
package com.zulipmobile.notifications
44

5-
import android.content.Context
65
import android.graphics.Bitmap
76
import android.graphics.BitmapFactory
8-
import android.text.Spannable
9-
import android.text.SpannableStringBuilder
10-
import android.text.TextUtils
11-
import android.text.style.StyleSpan
127
import android.util.Log
13-
import android.util.TypedValue
14-
import androidx.core.app.NotificationCompat
158
import java.io.IOException
169
import java.io.InputStream
1710
import java.net.URL
18-
import java.util.*
1911

2012
@JvmField
2113
val TAG = "ZulipNotif"
2214

23-
/**
24-
* All Zulip messages we're showing in notifications.
25-
*
26-
* Each key identifies a conversation; see [buildKeyString].
27-
*
28-
* Each value is the messages in the conversation, in the order we
29-
* received them.
30-
*/
31-
class ConversationMap : LinkedHashMap<String, MutableList<MessageFcmMessage>>()
32-
3315
fun fetchBitmap(url: URL): Bitmap? {
3416
return try {
3517
val connection = url.openConnection()
@@ -41,101 +23,3 @@ fun fetchBitmap(url: URL): Bitmap? {
4123
null
4224
}
4325
}
44-
45-
fun sizedURL(context: Context, url: URL, dpSize: Float): URL {
46-
// From http://stackoverflow.com/questions/4605527/
47-
val r = context.resources
48-
val px = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
49-
dpSize, r.displayMetrics)
50-
val query = if (url.query != null) "${url.query}&s=$px" else "s=$px"
51-
return URL(url, "?$query")
52-
}
53-
54-
fun buildNotificationContent(conversations: ConversationMap, inboxStyle: NotificationCompat.InboxStyle) {
55-
for (conversation in conversations.values) {
56-
// TODO ensure latest sender is shown last? E.g. Gmail-style A, B, ..., A.
57-
val seenSenders = HashSet<String>()
58-
val names = ArrayList<String>()
59-
for (message in conversation) {
60-
if (seenSenders.contains(message.sender.email))
61-
continue;
62-
seenSenders.add(message.sender.email)
63-
names.add(message.sender.fullName)
64-
}
65-
66-
val builder = SpannableStringBuilder()
67-
builder.append(TextUtils.join(", ", names))
68-
builder.setSpan(StyleSpan(android.graphics.Typeface.BOLD),
69-
0, builder.length, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
70-
builder.append(": ")
71-
builder.append(conversation.last().content)
72-
inboxStyle.addLine(builder)
73-
}
74-
}
75-
76-
fun extractTotalMessagesCount(conversations: ConversationMap): Int {
77-
var totalNumber = 0
78-
for ((_, value) in conversations) {
79-
totalNumber += value.size
80-
}
81-
return totalNumber
82-
}
83-
84-
/**
85-
* Formats -
86-
* stream message - fullName:streamName:'stream'
87-
* group message - fullName:Recipients:'group'
88-
* private message - fullName:Email:'private'
89-
*/
90-
private fun buildKeyString(fcmMessage: MessageFcmMessage): String {
91-
val recipient = fcmMessage.recipient
92-
return when (recipient) {
93-
is Recipient.Stream -> String.format("%s:stream", recipient.stream)
94-
is Recipient.GroupPm -> String.format("%s:group", recipient.getPmUsersString())
95-
is Recipient.Pm -> String.format("%s:private", fcmMessage.sender.email)
96-
}
97-
}
98-
99-
fun extractNames(conversations: ConversationMap): ArrayList<String> {
100-
val namesSet = LinkedHashSet<String>()
101-
for (fcmMessages in conversations.values) {
102-
for (fcmMessage in fcmMessages) {
103-
namesSet.add(fcmMessage.sender.fullName)
104-
}
105-
}
106-
return ArrayList(namesSet)
107-
}
108-
109-
fun addConversationToMap(fcmMessage: MessageFcmMessage, conversations: ConversationMap) {
110-
val key = buildKeyString(fcmMessage)
111-
var messages: MutableList<MessageFcmMessage>? = conversations[key]
112-
if (messages == null) {
113-
messages = ArrayList()
114-
}
115-
messages.add(fcmMessage)
116-
conversations[key] = messages
117-
}
118-
119-
fun removeMessagesFromMap(conversations: ConversationMap, removeFcmMessage: RemoveFcmMessage) {
120-
// We don't have the information to compute what key we ought to find each message under,
121-
// so just walk the whole thing. If the user has >100 notifications, this linear scan
122-
// won't be their worst problem anyway...
123-
//
124-
// TODO redesign this whole data structure, for many reasons.
125-
val it = conversations.values.iterator()
126-
while (it.hasNext()) {
127-
val messages: MutableList<MessageFcmMessage> = it.next()
128-
for (i in messages.indices.reversed()) {
129-
if (removeFcmMessage.messageIds.contains(messages[i].zulipMessageId)) {
130-
messages.removeAt(i)
131-
}
132-
}
133-
if (messages.isEmpty()) {
134-
it.remove()
135-
}
136-
}
137-
}
138-
139-
fun clearConversations(conversations: ConversationMap) {
140-
conversations.clear()
141-
}

android/app/src/main/java/com/zulipmobile/notifications/NotificationIntentService.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,11 @@ protected void onHandleIntent(Intent intent) {
2121
if (!(applicationContext instanceof MainApplication)) {
2222
return;
2323
}
24-
final ConversationMap conversations =
25-
((MainApplication) applicationContext).getConversations();
2624
if (ACTION_VIEW.equals(intent.getAction())) {
2725
final Bundle data = intent.getBundleExtra(EXTRA_NOTIFICATION_DATA);
28-
FCMPushNotifications.onOpened((ReactApplication) getApplication(), conversations, data);
26+
FCMPushNotifications.onOpened((ReactApplication) getApplication(), data);
2927
} else if (ACTION_CLEAR.equals(intent.getAction())) {
30-
FCMPushNotifications.onClear(this, conversations);
28+
// TODO implement dismiss
3129
}
3230
}
3331
}

0 commit comments

Comments
 (0)