Skip to content

Commit 8ff2b3d

Browse files
committed
UPDATE: Minor tweaks.
1 parent ae4ddcc commit 8ff2b3d

File tree

1 file changed

+39
-45
lines changed

1 file changed

+39
-45
lines changed

src/main/java/com/oasisfeng/nevo/decorators/wechat/WeChatDecorator.kt

Lines changed: 39 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -110,48 +110,43 @@ class WeChatDecorator : NevoDecoratorService() {
110110

111111
public override fun apply(evolving: MutableStatusBarNotification): Boolean {
112112
val n = evolving.notification
113-
if (n.flags and FLAG_GROUP_SUMMARY != 0) {
113+
val flags = n.flags
114+
if (flags and FLAG_GROUP_SUMMARY != 0) {
114115
n.extras.putCharSequence(EXTRA_SUB_TEXT, getText(when(n.group) {
115116
GROUP_GROUP -> R.string.header_group_chat
116117
GROUP_BOT -> R.string.header_bot_message
117-
else -> return false }))
118+
else -> return false }))
118119
return true
119120
}
120121
val extras = n.extras
121122
val title = extras.getCharSequence(EXTRA_TITLE)
123+
val channel = n.channelId
122124
if (title.isNullOrEmpty()) return false.also { Log.e(TAG, "Title is missing: $evolving") }
123-
124-
val flags = n.flags
125-
val channelId = n.channelId
126-
if (flags and FLAG_ONGOING_EVENT != 0 && channelId == CHANNEL_VOIP)
127-
return false
125+
if (flags and FLAG_ONGOING_EVENT != 0 && channel == CHANNEL_VOIP) return false
128126

129127
n.color = PRIMARY_COLOR // Tint the small icon
130128
extras.putBoolean(EXTRA_SHOW_WHEN, true)
131129
if (isEnabled(mPrefKeyWear)) n.flags = n.flags and FLAG_LOCAL_ONLY.inv() // Remove FLAG_LOCAL_ONLY
132-
if (n.tickerText == null /* Legacy misc. notifications */ || CHANNEL_MISC == channelId) {
133-
if (channelId == null) n.channelId = CHANNEL_MISC
130+
if (n.tickerText == null /* Legacy misc. notifications */ || channel == CHANNEL_MISC) {
131+
if (channel == null) n.channelId = CHANNEL_MISC
134132
n.group = GROUP_MISC // Avoid being auto-grouped
135133
if (evolving.id == NID_LOGIN_CONFIRMATION)
136134
n.timeoutAfter = (5 * 60000).toLong() // The actual timeout for login confirmation is a little shorter than 5 minutes.
137135
Log.d(TAG, "Skip further process for non-conversation notification: $title") // E.g. web login confirmation notification.
138-
return n.flags and FLAG_FOREGROUND_SERVICE == 0
136+
return flags and FLAG_FOREGROUND_SERVICE == 0
139137
}
140138
val contentText = extras.getCharSequence(EXTRA_TEXT) ?: return true
141139

142140
val inputHistory = extras.getCharSequenceArray(EXTRA_REMOTE_INPUT_HISTORY)
143141
if (inputHistory != null || extras.getBoolean(EXTRA_SILENT_RECAST)) n.flags = n.flags or FLAG_ONLY_ALERT_ONCE
144142

145-
// WeChat previously uses dynamic counter starting from 4097 as notification ID, which is reused after cancelled by WeChat itself,
146-
// causing conversation duplicate or overwritten notifications.
147143
val profile = evolving.user
148-
val conversation = mConversationManager.getOrCreateConversation(profile, evolving.originalId)
149-
conversation.icon = IconCompat.createFromIcon(this, n.getLargeIcon() ?: n.smallIcon)
150-
conversation.title = title
151-
conversation.summary = contentText
152-
conversation.ticker = n.tickerText
153-
conversation.timestamp = n.`when`
154-
conversation.ext = if (IGNORE_CAR_EXTENDER) null else CarExtender(n).unreadConversation
144+
val conversation = mConversationManager.getOrCreateConversation(profile, evolving.originalId).also {
145+
it.icon = IconCompat.createFromIcon(this, n.getLargeIcon() ?: n.smallIcon)
146+
it.title = title; it.summary = contentText; it.ticker = n.tickerText; it.timestamp = n.`when`
147+
it.ext = if (IGNORE_CAR_EXTENDER) null else CarExtender(n).unreadConversation
148+
}
149+
155150
val originalKey = evolving.originalKey
156151
var messaging = mMessagingBuilder.buildFromConversation(conversation, evolving)
157152
if (messaging == null) // EXTRA_TEXT will be written in buildFromArchive()
@@ -173,20 +168,20 @@ class WeChatDecorator : NevoDecoratorService() {
173168
if (latch.await(100, MILLISECONDS)) {
174169
if (BuildConfig.DEBUG) Log.d(TAG, "Conversation ID retrieved: " + conversation.id)
175170
} else Log.w(TAG, "Timeout retrieving conversation ID")
176-
} catch (ignored: InterruptedException) {}
177-
} catch (ignored: PendingIntent.CanceledException) {}
171+
} catch (_: InterruptedException) {}
172+
} catch (_: PendingIntent.CanceledException) {}
178173

179174
val cid = conversation.id
180-
if (cid != null) {
181-
val type = when {
182-
cid.endsWith("@chatroom") || cid.endsWith("@im.chatroom") -> Conversation.TYPE_GROUP_CHAT // @im.chatroom is WeWork
183-
cid.startsWith("gh_") || cid == KEY_SERVICE_MESSAGE -> Conversation.TYPE_BOT_MESSAGE
184-
cid.endsWith("@openim") -> Conversation.TYPE_DIRECT_MESSAGE
185-
else -> Conversation.TYPE_UNKNOWN }
186-
conversation.setType(type)
187-
} else if (conversation.isTypeUnknown())
188-
conversation.setType(guessConversationType(conversation))
189-
if (SDK_INT >= VERSION_CODES.R && inputHistory != null) { // EXTRA_REMOTE_INPUT_HISTORY is not longer supported on Android R.
175+
if (cid == null) {
176+
if (conversation.isTypeUnknown())
177+
conversation.setType(guessConversationType(conversation))
178+
} else conversation.setType(when {
179+
cid.endsWith("@chatroom") || cid.endsWith("@im.chatroom") -> Conversation.TYPE_GROUP_CHAT // @im.chatroom is WeWork
180+
cid.startsWith("gh_") || cid == KEY_SERVICE_MESSAGE -> Conversation.TYPE_BOT_MESSAGE
181+
cid.endsWith("@openim") -> Conversation.TYPE_DIRECT_MESSAGE
182+
else -> Conversation.TYPE_UNKNOWN
183+
})
184+
if (SDK_INT >= VERSION_CODES.R && inputHistory != null) { // EXTRA_REMOTE_INPUT_HISTORY is no longer supported on Android R.
190185
for (i in inputHistory.indices.reversed()) // Append them to messages in MessagingStyle.
191186
messages.add(NotificationCompat.MessagingStyle.Message(inputHistory[i], 0L, null as Person?))
192187
extras.remove(EXTRA_REMOTE_INPUT_HISTORY)
@@ -196,26 +191,25 @@ class WeChatDecorator : NevoDecoratorService() {
196191
messaging.conversationTitle = getString(R.string.header_service_message) // A special header for this non-group conversation with multiple senders
197192
n.group = GROUP_BOT
198193
} else n.group = if (isGroupChat) GROUP_GROUP else if (conversation.isBotMessage()) GROUP_BOT else GROUP_DIRECT
199-
if (isGroupChat && mUseExtraChannels && CHANNEL_DND != channelId) n.channelId = CHANNEL_GROUP_CONVERSATION
200-
else if (channelId == null) n.channelId = CHANNEL_MESSAGE // WeChat versions targeting O+ have its own channel for message }
194+
if (isGroupChat && mUseExtraChannels && channel != CHANNEL_DND) n.channelId = CHANNEL_GROUP_CONVERSATION
195+
else if (channel == null) n.channelId = CHANNEL_MESSAGE // WeChat versions targeting O+ have its own channel for message }
201196
if (isGroupChat) messaging.setGroupConversation(true).conversationTitle = title
202197
MessagingBuilder.flatIntoExtras(messaging, extras)
203198
extras.putString(EXTRA_TEMPLATE, TEMPLATE_MESSAGING)
204-
if (cid != null) {
205-
val shortcutId = buildShortcutId(cid)
206-
val shortcutReady = mAgentShortcuts.updateShortcutIfNeeded(shortcutId, conversation, profile)
207-
if (shortcutReady) n.shortcutId = shortcutId
208-
if (SDK_INT >= Q) {
209-
n.locusId = LocusId(shortcutId)
210-
if (SDK_INT > Q && shortcutReady) // Shortcut does not use conversation ID if it is absent.
211-
setBubbleMetadata(n, conversation, if (conversation.id != null) shortcutId else null)
212-
}
213-
}
199+
200+
if (SDK_INT > Q) maybeAddBubbleMetadata(n, conversation, profile)
214201
return true
215202
}
216203

217-
@RequiresApi(VERSION_CODES.R) private fun setBubbleMetadata(n: MutableNotification, conversation: Conversation, shortcut_id: String?) {
218-
val builder = if (shortcut_id != null) BubbleMetadata.Builder(shortcut_id) // WeChat does not met the requirement of bubble on Android Q: "documentLaunchMode=always"
204+
@RequiresApi(VERSION_CODES.R)
205+
private fun maybeAddBubbleMetadata(n: MutableNotification, conversation: Conversation, profile: UserHandle) {
206+
if (! conversation.isChat() || conversation.isBotMessage()) return
207+
val cid = conversation.id ?: return
208+
val shortcutId = buildShortcutId(cid)
209+
val shortcutReady = mAgentShortcuts.updateShortcutIfNeeded(shortcutId, conversation, profile)
210+
if (shortcutReady) n.shortcutId = shortcutId
211+
n.locusId = LocusId(shortcutId)
212+
val builder = if (shortcutReady) BubbleMetadata.Builder(shortcutId)
219213
else BubbleMetadata.Builder(n.contentIntent, convertToAdaptiveIcon(this, conversation.icon!!))
220214
n.bubbleMetadata = builder.setDesiredHeight(DESIRED_BUBBLE_EXPANDED_HEIGHT).build()
221215
}

0 commit comments

Comments
 (0)