Commit bc419e39 by zhangchengbo

fix:添加短信角标数字提醒功能

parent b0bdfbdb
......@@ -5,7 +5,6 @@ import android.app.Activity
import android.app.role.RoleManager
import android.content.Intent
import android.database.Cursor
import android.net.Uri
import android.os.Build
import android.os.Bundle
import android.provider.Telephony
......@@ -25,6 +24,7 @@ import com.secspace.sms.helpers.THREAD_ID
import com.secspace.sms.helpers.THREAD_TITLE
import com.secspace.sms.models.*
import com.secspace.sms.util.PhoneUtils
import com.secspace.sms.util.SmsCountUtil
import com.simplemobiletools.commons.dialogs.PermissionRequiredDialog
import com.simplemobiletools.commons.extensions.*
import com.simplemobiletools.commons.helpers.*
......@@ -379,10 +379,14 @@ class MainActivity : SimpleActivity() {
// Log.e(TAG, "----MainActivity----handleMessage allConversations:${GsonUtil.parseListToJson(allConversations)}")
setupConversations(allConversations)
val readResult = conversations.count { !it.read }
lifecycleScope.launch(Dispatchers.Main) {
setBadgeNum(readResult)
}
val listMessage = messagesDB.getAll()
val readResult = listMessage.count { !it.read }
// lifecycleScope.launch(Dispatchers.Main) {
SmsCountUtil.setBadgeNum(this@MainActivity, readResult)
config.badgeTotalNumber = readResult
// }
if (config.appRunCount == 1) {
conversations.map { it.threadId }.forEach { threadId ->
......@@ -516,19 +520,6 @@ class MainActivity : SimpleActivity() {
}.start()
}
//https://developer.honor.com/cn/forum/topicdetail/topicid-3540765949296640
private fun setBadgeNum(num: Int) {
try {
val bunlde = Bundle()
bunlde.putString("package", "com.secspace.sms") // com.test.badge is your package name
bunlde.putString("class", "com.secspace.sms.activities.SplashActivity.Orange") // com.test. badge.MainActivity is your apk main activity
bunlde.putInt("badgenumber", num)
this.contentResolver.call(Uri.parse("content://com.hihonor.android.launcher.settings/badge/"), "change_badge", null, bunlde)
} catch (e: java.lang.Exception) {
e.printStackTrace()
}
}
@SuppressLint("NotifyDataSetChanged")
private fun notifyDatasetChanged() {
getOrCreateConversationsAdapter().notifyDataSetChanged()
......
......@@ -115,4 +115,9 @@ class Config(context: Context) : BaseConfig(context) {
var isArchiveAvailable: Boolean
get() = prefs.getBoolean(IS_ARCHIVE_AVAILABLE, true)
set(isArchiveAvailable) = prefs.edit().putBoolean(IS_ARCHIVE_AVAILABLE, isArchiveAvailable).apply()
var badgeTotalNumber: Int
get() = prefs.getInt(BADGE_TOTAL_NUMBER, 0)
set(badgeTotalNumber) = prefs.edit().putInt(BADGE_TOTAL_NUMBER, badgeTotalNumber).apply()
}
......@@ -45,6 +45,7 @@ const val USE_RECYCLE_BIN = "use_recycle_bin"
const val LAST_RECYCLE_BIN_CHECK = "last_recycle_bin_check"
const val IS_RECYCLE_BIN = "is_recycle_bin"
const val IS_ARCHIVE_AVAILABLE = "is_archive_available"
const val BADGE_TOTAL_NUMBER = "badge_total_number"
private const val PATH = "com.secspace.sms.action."
const val MARK_AS_READ = PATH + "mark_as_read"
......
......@@ -3,17 +3,20 @@ package com.secspace.sms.receivers
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.database.Cursor
import android.os.Handler
import android.os.Looper
import android.provider.Telephony
import com.secspace.log.Log
import com.secspace.sms.extensions.*
import com.secspace.sms.helpers.Config
import com.secspace.sms.helpers.RecentsHelper
import com.secspace.sms.helpers.refreshMessages
import com.secspace.sms.models.Conversation
import com.secspace.sms.models.Message
import com.secspace.sms.models.RecentCall
import com.secspace.sms.util.PhoneUtils
import com.secspace.sms.util.SmsCountUtil
import com.simplemobiletools.commons.extensions.baseConfig
import com.simplemobiletools.commons.extensions.getMyContactsCursor
import com.simplemobiletools.commons.extensions.hasPermission
......@@ -23,12 +26,10 @@ import com.simplemobiletools.commons.helpers.SimpleContactsHelper
import com.simplemobiletools.commons.helpers.ensureBackgroundThread
import com.simplemobiletools.commons.models.PhoneNumber
import com.simplemobiletools.commons.models.SimpleContact
import com.simplemobiletools.commons.util.GsonUtil
class SmsReceiver : BroadcastReceiver() {
private val TAG: String = "shuju"
// private var findPhoneNumber = false
private var lastNumber = ""
override fun onReceive(context: Context, intent: Intent) {
......@@ -44,7 +45,7 @@ class SmsReceiver : BroadcastReceiver() {
val read = 0
val subscriptionId = intent.getIntExtra("subscription", -1)
val privateCursor = context.getMyContactsCursor(favoritesOnly = false, withPhoneNumbersOnly = true)
val privateCursor: Cursor? = context.getMyContactsCursor(favoritesOnly = false, withPhoneNumbersOnly = true)
ensureBackgroundThread {
messages.forEach {
address = it.originatingAddress ?: ""
......@@ -61,11 +62,15 @@ class SmsReceiver : BroadcastReceiver() {
val simpleContactsHelper = SimpleContactsHelper(context)
simpleContactsHelper.exists(address, privateCursor) { exists ->
if (exists) {
handleMessage(context, address, subject, body, date, read, threadId, type, subscriptionId, status)
handleMessage(context, address, subject, body, date, read, threadId, type, subscriptionId, status) {
badgeTotalNumber(context)
}
}
}
} else {
handleMessage(context, address, subject, body, date, read, threadId, type, subscriptionId, status)
handleMessage(context, address, subject, body, date, read, threadId, type, subscriptionId, status) {
badgeTotalNumber(context)
}
}
}
}
......@@ -103,14 +108,14 @@ class SmsReceiver : BroadcastReceiver() {
//从最近通话记录中找到了号码 进行掩码操作
if (findResult > 0) {
PhoneUtils.phoneNumberFormat(phoneNumber) { itNumber ->
/* Log.d(
TAG,
"----SmsReceiver----手机号掩码操作 itNumber:${itNumber} threadId:$threadId context.getThreadId:${
context.getThreadId(
phoneNumber
)
}"
)*/
/* Log.d(
TAG,
"----SmsReceiver----手机号掩码操作 itNumber:${itNumber} threadId:$threadId context.getThreadId:${
context.getThreadId(
phoneNumber
)
}"
)*/
lastNumber = itNumber
}
}
......@@ -131,12 +136,12 @@ class SmsReceiver : BroadcastReceiver() {
threadId: Long,
type: Int,
subscriptionId: Int,
status: Int
status: Int,
confirmCallBack: () -> Unit
) {
if (isMessageFilteredOut(context, body)) {
return
}
val photoUri = SimpleContactsHelper(context).getPhotoUriFromPhoneNumber(address)
val bitmap = context.getNotificationBitmap(photoUri)
Handler(Looper.getMainLooper()).post {
......@@ -226,6 +231,7 @@ class SmsReceiver : BroadcastReceiver() {
}
}
}
confirmCallBack.invoke()
}
private fun isMessageFilteredOut(context: Context, body: String): Boolean {
......@@ -237,14 +243,18 @@ class SmsReceiver : BroadcastReceiver() {
return false
}
private fun getIndexById(userList: List<Conversation>, id: Long): Int {
for (i in userList.indices) {
if (userList[i].threadId === id) {
return i // 找到匹配的ID,返回下标
}
}
return -1 // 如果未找到,返回-1
/**
* 统计未读消息数量
*
* 存储值在 Mainactivity.kt 完成
* 此处只做获取数值操作
* 1.未获取到值则表明没有未读短信,直接+1显示
* 2.获取到值也要数量+1 显示
*/
private fun badgeTotalNumber(context: Context) {
val readResult: Int = Config.newInstance(context).badgeTotalNumber
SmsCountUtil.setBadgeNum(context, readResult + 1)
Log.d(TAG, "badgeTotalNumber readResult = $readResult ")
}
}
......
package com.secspace.sms.util
import android.content.Context
import android.net.Uri
import android.os.Bundle
data object SmsCountUtil {
//https://developer.honor.com/cn/forum/topicdetail/topicid-3540765949296640
fun setBadgeNum(mContext : Context,num: Int) {
try {
val bunlde = Bundle()
bunlde.putString("package", "com.secspace.sms") // com.test.badge is your package name
bunlde.putString("class", "com.secspace.sms.activities.SplashActivity.Orange") // com.test. badge.MainActivity is your apk main activity
bunlde.putInt("badgenumber", num)
mContext.contentResolver.call(Uri.parse("content://com.hihonor.android.launcher.settings/badge/"), "change_badge", null, bunlde)
} catch (e: java.lang.Exception) {
e.printStackTrace()
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment