Skip to content

Commit ae4ddcc

Browse files
committed
UPDATE: Drop support for Android 7.x and old version of WeChat.
1 parent 1ec73ce commit ae4ddcc

File tree

5 files changed

+100
-172
lines changed

5 files changed

+100
-172
lines changed

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

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,31 +16,30 @@
1616

1717
package com.oasisfeng.nevo.decorators.wechat
1818

19-
import android.annotation.SuppressLint
20-
import android.content.*
19+
import android.content.ComponentName
20+
import android.content.Context
21+
import android.content.Intent
22+
import android.content.LocusId
2123
import android.content.pm.LauncherApps
2224
import android.content.pm.PackageManager
2325
import android.content.pm.PackageManager.GET_ACTIVITIES
2426
import android.content.pm.ShortcutInfo
2527
import android.content.pm.ShortcutManager
2628
import android.graphics.drawable.Icon.TYPE_RESOURCE
2729
import android.os.Build.VERSION.SDK_INT
28-
import android.os.Build.VERSION_CODES.N
29-
import android.os.Build.VERSION_CODES.N_MR1
3030
import android.os.Build.VERSION_CODES.Q
3131
import android.os.Process
3232
import android.os.UserHandle
3333
import android.os.UserManager
3434
import android.util.ArrayMap
3535
import android.util.Log
3636
import android.util.LruCache
37-
import androidx.annotation.RequiresApi
3837
import androidx.core.content.getSystemService
3938
import com.oasisfeng.nevo.decorators.wechat.ConversationManager.Conversation
4039
import com.oasisfeng.nevo.decorators.wechat.IconHelper.toLocalAdaptiveIcon
4140
import java.lang.reflect.Method
4241

43-
@RequiresApi(N_MR1) class AgentShortcuts(private val context: Context) {
42+
class AgentShortcuts(private val context: Context) {
4443

4544
companion object {
4645
private const val FLAG_ALLOW_EMBEDDED = -0x80000000
@@ -49,7 +48,7 @@ import java.lang.reflect.Method
4948

5049
/** @return true if shortcut is ready */
5150
private fun updateShortcut(id: String, conversation: Conversation, agentContext: Context): Boolean {
52-
if (SDK_INT >= N && agentContext.getSystemService(UserManager::class.java)?.isUserUnlocked == false) return false // Shortcuts cannot be changed if user is locked.
51+
if (agentContext.getSystemService(UserManager::class.java)?.isUserUnlocked == false) return false // Shortcuts cannot be changed if user is locked.
5352

5453
val activity = agentContext.packageManager.resolveActivity(Intent(Intent.ACTION_MAIN) // Use agent context to resolve in proper user.
5554
.addCategory(Intent.CATEGORY_LAUNCHER).setPackage(AGENT_PACKAGE), 0)?.activityInfo?.name
@@ -71,9 +70,9 @@ import java.lang.reflect.Method
7170
?: try { context.packageManager.getPackageInfo(AGENT_PACKAGE, GET_ACTIVITIES).activities
7271
.firstOrNull { it.enabled && it.flags.and(FLAG_ALLOW_EMBEDDED) != 0 }?.name ?: "" }
7372
catch (e: PackageManager.NameNotFoundException) { "" }.also { mAgentBubbleActivity = it }) // "" to indicate N/A
74-
if (bubbleActivity.isNotEmpty()) {
73+
if (SDK_INT >= Q && bubbleActivity.isNotEmpty())
7574
Intent(Intent.ACTION_VIEW_LOCUS).putExtra(Intent.EXTRA_LOCUS_ID, id).setClassName(AGENT_PACKAGE, bubbleActivity)
76-
} else Intent().setClassName(AGENT_PACKAGE, activity)
75+
else Intent().setClassName(AGENT_PACKAGE, activity)
7776
}
7877

7978
val shortcut = ShortcutInfo.Builder(agentContext, id).setActivity(ComponentName(AGENT_PACKAGE, activity))
@@ -91,12 +90,12 @@ import java.lang.reflect.Method
9190
else false.also { Log.e(TAG, "Unexpected rate limit.") }
9291
}
9392

94-
private fun createAgentContext(profile: UserHandle): Context?
95-
= try {
93+
private fun createAgentContext(profile: UserHandle): Context? =
94+
try {
9695
if (profile == Process.myUserHandle()) context.createPackageContext(AGENT_PACKAGE, 0)
97-
else mMethodCreatePackageContextAsUser?.invoke(context, AGENT_PACKAGE, 0, profile) as? Context }
98-
catch (e: PackageManager.NameNotFoundException) { null }
99-
catch (e: RuntimeException) { null.also { Log.e(TAG, "Error creating context for agent in user ${profile.hashCode()}", e) }}
96+
else mMethodCreatePackageContextAsUser?.invoke(context, AGENT_PACKAGE, 0, profile) as? Context
97+
} catch (e: PackageManager.NameNotFoundException) { null
98+
} catch (e: RuntimeException) { null.also { Log.e(TAG, "Error creating context for agent in user ${profile.hashCode()}", e) }}
10099

101100
/** @return whether shortcut is ready */
102101
fun updateShortcutIfNeeded(id: String, conversation: Conversation, profile: UserHandle): Boolean {

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

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,10 @@ import android.graphics.Bitmap
2222
import android.graphics.Canvas
2323
import android.graphics.drawable.AdaptiveIconDrawable
2424
import android.graphics.drawable.Icon
25-
import android.os.Build.VERSION.SDK_INT
26-
import android.os.Build.VERSION_CODES.O
27-
import androidx.annotation.RequiresApi
2825
import androidx.core.content.getSystemService
2926
import androidx.core.graphics.drawable.IconCompat
3027

31-
@RequiresApi(O) object IconHelper {
28+
object IconHelper {
3229

3330
fun convertToAdaptiveIcon(context: Context, source: IconCompat): Icon =
3431
if (source.type == Icon.TYPE_RESOURCE) source.toIcon(null)
@@ -46,9 +43,7 @@ import androidx.core.graphics.drawable.IconCompat
4643
}
4744
}
4845

49-
fun IconCompat.toLocalAdaptiveIcon(context: Context, sm: ShortcutManager): Icon = when {
50-
SDK_INT < O -> toIcon(null)
51-
type == Icon.TYPE_ADAPTIVE_BITMAP -> toIcon(null)
52-
else -> Icon.createWithAdaptiveBitmap(drawableToBitmap(context, sm, this))
53-
}
46+
fun IconCompat.toLocalAdaptiveIcon(context: Context, sm: ShortcutManager): Icon =
47+
if (type == IconCompat.TYPE_ADAPTIVE_BITMAP) toIcon(null)
48+
else Icon.createWithAdaptiveBitmap(drawableToBitmap(context, sm, this))
5449
}

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

Lines changed: 18 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,15 @@ import android.annotation.SuppressLint
2020
import android.app.Notification
2121
import android.app.NotificationManager
2222
import android.app.PendingIntent
23+
import android.app.PendingIntent.FLAG_IMMUTABLE
24+
import android.app.PendingIntent.FLAG_UPDATE_CURRENT
2325
import android.app.RemoteInput
2426
import android.content.BroadcastReceiver
2527
import android.content.Context
2628
import android.content.Intent
2729
import android.content.IntentFilter
2830
import android.net.Uri
2931
import android.os.Build.VERSION.SDK_INT
30-
import android.os.Build.VERSION_CODES.N
31-
import android.os.Build.VERSION_CODES.O
3232
import android.os.Build.VERSION_CODES.P
3333
import android.os.Bundle
3434
import android.os.Process
@@ -125,7 +125,7 @@ internal class MessagingBuilder(private val mContext: Context, private val mCont
125125
if (onRead != null) mMarkReadPendingIntents[sbn.key] = onRead // Mapped by evolved key,
126126
val messages = buildMessages(conversation)
127127
val remoteInput = ext.remoteInput
128-
if (SDK_INT >= N && onReply != null && remoteInput != null && conversation.isChat()) {
128+
if (onReply != null && remoteInput != null && conversation.isChat()) {
129129
val inputHistory = n.extras.getCharSequenceArray(Notification.EXTRA_REMOTE_INPUT_HISTORY)
130130
val proxy = proxyDirectReply(conversation.nid, sbn, onReply, remoteInput, inputHistory)
131131
val replyRemoteInput = RemoteInput.Builder(remoteInput.resultKey).addExtras(remoteInput.extras)
@@ -154,9 +154,9 @@ internal class MessagingBuilder(private val mContext: Context, private val mCont
154154
.putExtra(EXTRA_REPLY_ACTION, onReply).putExtra(EXTRA_RESULT_KEY, remoteInput.resultKey)
155155
.putExtra(EXTRA_ORIGINAL_KEY, sbn.originalKey).putExtra(EXTRA_CONVERSATION_ID, cid)
156156
.putExtra(Intent.EXTRA_USER, sbn.user)
157-
if (SDK_INT >= N && inputHistory != null)
157+
if (inputHistory != null)
158158
proxy.putCharSequenceArrayListExtra(Notification.EXTRA_REMOTE_INPUT_HISTORY, arrayListOf(*inputHistory))
159-
return PendingIntent.getBroadcast(mContext, 0, proxy.setPackage(mContext.packageName), PendingIntent.FLAG_UPDATE_CURRENT)
159+
return PendingIntent.getBroadcast(mContext, 0, proxy.setPackage(mContext.packageName), FLAG_UPDATE_CURRENT or FLAG_IMMUTABLE)
160160
}
161161

162162
private val mReplyReceiver: BroadcastReceiver = object : BroadcastReceiver() { override fun onReceive(context: Context, proxy: Intent) {
@@ -182,22 +182,20 @@ internal class MessagingBuilder(private val mContext: Context, private val mCont
182182
RemoteInput.addResultsToIntent(arrayOf(RemoteInput.Builder(resultKey).build()), proxy, results)
183183
} ?: input
184184

185-
val history = if (SDK_INT >= N) proxy.getCharSequenceArrayListExtra(Notification.EXTRA_REMOTE_INPUT_HISTORY) else null
185+
val history = proxy.getCharSequenceArrayListExtra(Notification.EXTRA_REMOTE_INPUT_HISTORY)
186186
try {
187187
val inputData = addTargetPackageAndWakeUp(replyAction)
188188
inputData.clipData = proxy.clipData
189189
replyAction.send(mContext, 0, inputData, PendingIntent.OnFinished { pendingIntent: PendingIntent, intent: Intent, _: Int, _: String?, _: Bundle? ->
190190
if (BuildConfig.DEBUG) Log.d(TAG, "Reply sent: " + intent.toUri(0))
191-
if (SDK_INT >= N) {
192-
val addition = Bundle()
193-
val inputs: Array<CharSequence>
194-
val toCurrentUser = Process.myUserHandle() == pendingIntent.creatorUserHandle
195-
inputs = if (toCurrentUser && context.packageManager.queryBroadcastReceivers(intent, 0).isEmpty())
196-
arrayOf(context.getString(R.string.wechat_with_no_reply_receiver))
197-
else history?.apply { add(0, text) }?.toTypedArray() ?: arrayOf(text)
198-
addition.putCharSequenceArray(Notification.EXTRA_REMOTE_INPUT_HISTORY, inputs)
199-
mController.recastNotification(originalKey ?: key, addition)
200-
}
191+
val addition = Bundle()
192+
val inputs: Array<CharSequence>
193+
val toCurrentUser = Process.myUserHandle() == pendingIntent.creatorUserHandle
194+
inputs = if (toCurrentUser && context.packageManager.queryBroadcastReceivers(intent, 0).isEmpty())
195+
arrayOf(context.getString(R.string.wechat_with_no_reply_receiver))
196+
else history?.apply { add(0, text) }?.toTypedArray() ?: arrayOf(text)
197+
addition.putCharSequenceArray(Notification.EXTRA_REMOTE_INPUT_HISTORY, inputs)
198+
mController.recastNotification(originalKey ?: key, addition)
201199
markRead(key)
202200
}, null)
203201
} catch (e: PendingIntent.CanceledException) {
@@ -251,10 +249,9 @@ internal class MessagingBuilder(private val mContext: Context, private val mCont
251249
val bigText = StringBuilder().append(convs.summary).append("\nT:").append(convs.ticker)
252250
val messages = if (convs.ext != null) convs.ext!!.messages else null
253251
if (messages != null) for (msg in messages) bigText.append("\n").append(msg)
254-
val n = Notification.Builder(context).setSmallIcon(android.R.drawable.stat_sys_warning)
252+
val n = Notification.Builder(context, "Debug").setSmallIcon(android.R.drawable.stat_sys_warning)
255253
.setContentTitle(convs.id).setContentText(convs.ticker).setSubText(summary).setShowWhen(true)
256254
.setStyle(Notification.BigTextStyle().setBigContentTitle(convs.title).bigText(bigText.toString()))
257-
if (SDK_INT >= O) n.setChannelId("Debug")
258255
context.getSystemService(NotificationManager::class.java)
259256
.notify(if (convs.id != null) convs.id.hashCode() else convs.title.hashCode(), n.build())
260257
}
@@ -319,10 +316,8 @@ internal class MessagingBuilder(private val mContext: Context, private val mCont
319316

320317
fun flatIntoExtras(messaging: NotificationCompat.MessagingStyle, extras: Bundle) {
321318
val user = messaging.user
322-
if (user != null) {
323-
extras.putCharSequence(NotificationCompat.EXTRA_SELF_DISPLAY_NAME, user.name)
324-
if (SDK_INT >= P) extras.putParcelable(Notification.EXTRA_MESSAGING_PERSON, user.toNative()) // Not included in NotificationCompat
325-
}
319+
extras.putCharSequence(NotificationCompat.EXTRA_SELF_DISPLAY_NAME, user.name)
320+
if (SDK_INT >= P) extras.putParcelable(Notification.EXTRA_MESSAGING_PERSON, user.toNative()) // Not included in NotificationCompat
326321
if (messaging.conversationTitle != null)
327322
extras.putCharSequence(NotificationCompat.EXTRA_CONVERSATION_TITLE, messaging.conversationTitle)
328323
val messages = messaging.messages
@@ -344,7 +339,7 @@ internal class MessagingBuilder(private val mContext: Context, private val mCont
344339
}
345340
if (message.dataMimeType != null) putString(KEY_DATA_MIME_TYPE, message.dataMimeType)
346341
if (message.dataUri != null) putParcelable(KEY_DATA_URI, message.dataUri)
347-
if (SDK_INT >= O && !message.extras.isEmpty) putBundle(KEY_EXTRAS_BUNDLE, message.extras)
342+
if (! message.extras.isEmpty) putBundle(KEY_EXTRAS_BUNDLE, message.extras)
348343
//if (message.isRemoteInputHistory()) putBoolean(KEY_REMOTE_INPUT_HISTORY, message.isRemoteInputHistory());
349344
}
350345

0 commit comments

Comments
 (0)