Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
S
Simple-Sms
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
zhangchengbo
Simple-Sms
Commits
bc419e39
Commit
bc419e39
authored
Mar 04, 2025
by
zhangchengbo
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix:添加短信角标数字提醒功能
parent
b0bdfbdb
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
63 additions
and
33 deletions
+63
-33
MainActivity.kt
...c/main/kotlin/com/secspace/sms/activities/MainActivity.kt
+9
-18
Config.kt
app/src/main/kotlin/com/secspace/sms/helpers/Config.kt
+5
-0
Constants.kt
app/src/main/kotlin/com/secspace/sms/helpers/Constants.kt
+1
-0
SmsReceiver.kt
...src/main/kotlin/com/secspace/sms/receivers/SmsReceiver.kt
+25
-15
SmsCountUtil.kt
app/src/main/kotlin/com/secspace/sms/util/SmsCountUtil.kt
+23
-0
No files found.
app/src/main/kotlin/com/secspace/sms/activities/MainActivity.kt
View file @
bc419e39
...
@@ -5,7 +5,6 @@ import android.app.Activity
...
@@ -5,7 +5,6 @@ import android.app.Activity
import
android.app.role.RoleManager
import
android.app.role.RoleManager
import
android.content.Intent
import
android.content.Intent
import
android.database.Cursor
import
android.database.Cursor
import
android.net.Uri
import
android.os.Build
import
android.os.Build
import
android.os.Bundle
import
android.os.Bundle
import
android.provider.Telephony
import
android.provider.Telephony
...
@@ -25,6 +24,7 @@ import com.secspace.sms.helpers.THREAD_ID
...
@@ -25,6 +24,7 @@ import com.secspace.sms.helpers.THREAD_ID
import
com.secspace.sms.helpers.THREAD_TITLE
import
com.secspace.sms.helpers.THREAD_TITLE
import
com.secspace.sms.models.*
import
com.secspace.sms.models.*
import
com.secspace.sms.util.PhoneUtils
import
com.secspace.sms.util.PhoneUtils
import
com.secspace.sms.util.SmsCountUtil
import
com.simplemobiletools.commons.dialogs.PermissionRequiredDialog
import
com.simplemobiletools.commons.dialogs.PermissionRequiredDialog
import
com.simplemobiletools.commons.extensions.*
import
com.simplemobiletools.commons.extensions.*
import
com.simplemobiletools.commons.helpers.*
import
com.simplemobiletools.commons.helpers.*
...
@@ -379,10 +379,14 @@ class MainActivity : SimpleActivity() {
...
@@ -379,10 +379,14 @@ class MainActivity : SimpleActivity() {
// Log.e(TAG, "----MainActivity----handleMessage allConversations:${GsonUtil.parseListToJson(allConversations)}")
// Log.e(TAG, "----MainActivity----handleMessage allConversations:${GsonUtil.parseListToJson(allConversations)}")
setupConversations
(
allConversations
)
setupConversations
(
allConversations
)
val
readResult
=
conversations
.
count
{
!
it
.
read
}
lifecycleScope
.
launch
(
Dispatchers
.
Main
)
{
val
listMessage
=
messagesDB
.
getAll
()
setBadgeNum
(
readResult
)
}
val
readResult
=
listMessage
.
count
{
!
it
.
read
}
// lifecycleScope.launch(Dispatchers.Main) {
SmsCountUtil
.
setBadgeNum
(
this
@MainActivity
,
readResult
)
config
.
badgeTotalNumber
=
readResult
// }
if
(
config
.
appRunCount
==
1
)
{
if
(
config
.
appRunCount
==
1
)
{
conversations
.
map
{
it
.
threadId
}.
forEach
{
threadId
->
conversations
.
map
{
it
.
threadId
}.
forEach
{
threadId
->
...
@@ -516,19 +520,6 @@ class MainActivity : SimpleActivity() {
...
@@ -516,19 +520,6 @@ class MainActivity : SimpleActivity() {
}.
start
()
}.
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"
)
@SuppressLint
(
"NotifyDataSetChanged"
)
private
fun
notifyDatasetChanged
()
{
private
fun
notifyDatasetChanged
()
{
getOrCreateConversationsAdapter
().
notifyDataSetChanged
()
getOrCreateConversationsAdapter
().
notifyDataSetChanged
()
...
...
app/src/main/kotlin/com/secspace/sms/helpers/Config.kt
View file @
bc419e39
...
@@ -115,4 +115,9 @@ class Config(context: Context) : BaseConfig(context) {
...
@@ -115,4 +115,9 @@ class Config(context: Context) : BaseConfig(context) {
var
isArchiveAvailable
:
Boolean
var
isArchiveAvailable
:
Boolean
get
()
=
prefs
.
getBoolean
(
IS_ARCHIVE_AVAILABLE
,
true
)
get
()
=
prefs
.
getBoolean
(
IS_ARCHIVE_AVAILABLE
,
true
)
set
(
isArchiveAvailable
)
=
prefs
.
edit
().
putBoolean
(
IS_ARCHIVE_AVAILABLE
,
isArchiveAvailable
).
apply
()
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
()
}
}
app/src/main/kotlin/com/secspace/sms/helpers/Constants.kt
View file @
bc419e39
...
@@ -45,6 +45,7 @@ const val USE_RECYCLE_BIN = "use_recycle_bin"
...
@@ -45,6 +45,7 @@ const val USE_RECYCLE_BIN = "use_recycle_bin"
const
val
LAST_RECYCLE_BIN_CHECK
=
"last_recycle_bin_check"
const
val
LAST_RECYCLE_BIN_CHECK
=
"last_recycle_bin_check"
const
val
IS_RECYCLE_BIN
=
"is_recycle_bin"
const
val
IS_RECYCLE_BIN
=
"is_recycle_bin"
const
val
IS_ARCHIVE_AVAILABLE
=
"is_archive_available"
const
val
IS_ARCHIVE_AVAILABLE
=
"is_archive_available"
const
val
BADGE_TOTAL_NUMBER
=
"badge_total_number"
private
const
val
PATH
=
"com.secspace.sms.action."
private
const
val
PATH
=
"com.secspace.sms.action."
const
val
MARK_AS_READ
=
PATH
+
"mark_as_read"
const
val
MARK_AS_READ
=
PATH
+
"mark_as_read"
...
...
app/src/main/kotlin/com/secspace/sms/receivers/SmsReceiver.kt
View file @
bc419e39
...
@@ -3,17 +3,20 @@ package com.secspace.sms.receivers
...
@@ -3,17 +3,20 @@ package com.secspace.sms.receivers
import
android.content.BroadcastReceiver
import
android.content.BroadcastReceiver
import
android.content.Context
import
android.content.Context
import
android.content.Intent
import
android.content.Intent
import
android.database.Cursor
import
android.os.Handler
import
android.os.Handler
import
android.os.Looper
import
android.os.Looper
import
android.provider.Telephony
import
android.provider.Telephony
import
com.secspace.log.Log
import
com.secspace.log.Log
import
com.secspace.sms.extensions.*
import
com.secspace.sms.extensions.*
import
com.secspace.sms.helpers.Config
import
com.secspace.sms.helpers.RecentsHelper
import
com.secspace.sms.helpers.RecentsHelper
import
com.secspace.sms.helpers.refreshMessages
import
com.secspace.sms.helpers.refreshMessages
import
com.secspace.sms.models.Conversation
import
com.secspace.sms.models.Conversation
import
com.secspace.sms.models.Message
import
com.secspace.sms.models.Message
import
com.secspace.sms.models.RecentCall
import
com.secspace.sms.models.RecentCall
import
com.secspace.sms.util.PhoneUtils
import
com.secspace.sms.util.PhoneUtils
import
com.secspace.sms.util.SmsCountUtil
import
com.simplemobiletools.commons.extensions.baseConfig
import
com.simplemobiletools.commons.extensions.baseConfig
import
com.simplemobiletools.commons.extensions.getMyContactsCursor
import
com.simplemobiletools.commons.extensions.getMyContactsCursor
import
com.simplemobiletools.commons.extensions.hasPermission
import
com.simplemobiletools.commons.extensions.hasPermission
...
@@ -23,12 +26,10 @@ import com.simplemobiletools.commons.helpers.SimpleContactsHelper
...
@@ -23,12 +26,10 @@ import com.simplemobiletools.commons.helpers.SimpleContactsHelper
import
com.simplemobiletools.commons.helpers.ensureBackgroundThread
import
com.simplemobiletools.commons.helpers.ensureBackgroundThread
import
com.simplemobiletools.commons.models.PhoneNumber
import
com.simplemobiletools.commons.models.PhoneNumber
import
com.simplemobiletools.commons.models.SimpleContact
import
com.simplemobiletools.commons.models.SimpleContact
import
com.simplemobiletools.commons.util.GsonUtil
class
SmsReceiver
:
BroadcastReceiver
()
{
class
SmsReceiver
:
BroadcastReceiver
()
{
private
val
TAG
:
String
=
"shuju"
private
val
TAG
:
String
=
"shuju"
// private var findPhoneNumber = false
private
var
lastNumber
=
""
private
var
lastNumber
=
""
override
fun
onReceive
(
context
:
Context
,
intent
:
Intent
)
{
override
fun
onReceive
(
context
:
Context
,
intent
:
Intent
)
{
...
@@ -44,7 +45,7 @@ class SmsReceiver : BroadcastReceiver() {
...
@@ -44,7 +45,7 @@ class SmsReceiver : BroadcastReceiver() {
val
read
=
0
val
read
=
0
val
subscriptionId
=
intent
.
getIntExtra
(
"subscription"
,
-
1
)
val
subscriptionId
=
intent
.
getIntExtra
(
"subscription"
,
-
1
)
val
privateCursor
=
context
.
getMyContactsCursor
(
favoritesOnly
=
false
,
withPhoneNumbersOnly
=
true
)
val
privateCursor
:
Cursor
?
=
context
.
getMyContactsCursor
(
favoritesOnly
=
false
,
withPhoneNumbersOnly
=
true
)
ensureBackgroundThread
{
ensureBackgroundThread
{
messages
.
forEach
{
messages
.
forEach
{
address
=
it
.
originatingAddress
?:
""
address
=
it
.
originatingAddress
?:
""
...
@@ -61,11 +62,15 @@ class SmsReceiver : BroadcastReceiver() {
...
@@ -61,11 +62,15 @@ class SmsReceiver : BroadcastReceiver() {
val
simpleContactsHelper
=
SimpleContactsHelper
(
context
)
val
simpleContactsHelper
=
SimpleContactsHelper
(
context
)
simpleContactsHelper
.
exists
(
address
,
privateCursor
)
{
exists
->
simpleContactsHelper
.
exists
(
address
,
privateCursor
)
{
exists
->
if
(
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
{
}
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
)
}
}
}
}
}
}
}
...
@@ -131,12 +136,12 @@ class SmsReceiver : BroadcastReceiver() {
...
@@ -131,12 +136,12 @@ class SmsReceiver : BroadcastReceiver() {
threadId
:
Long
,
threadId
:
Long
,
type
:
Int
,
type
:
Int
,
subscriptionId
:
Int
,
subscriptionId
:
Int
,
status
:
Int
status
:
Int
,
confirmCallBack
:
()
->
Unit
)
{
)
{
if
(
isMessageFilteredOut
(
context
,
body
))
{
if
(
isMessageFilteredOut
(
context
,
body
))
{
return
return
}
}
val
photoUri
=
SimpleContactsHelper
(
context
).
getPhotoUriFromPhoneNumber
(
address
)
val
photoUri
=
SimpleContactsHelper
(
context
).
getPhotoUriFromPhoneNumber
(
address
)
val
bitmap
=
context
.
getNotificationBitmap
(
photoUri
)
val
bitmap
=
context
.
getNotificationBitmap
(
photoUri
)
Handler
(
Looper
.
getMainLooper
()).
post
{
Handler
(
Looper
.
getMainLooper
()).
post
{
...
@@ -226,6 +231,7 @@ class SmsReceiver : BroadcastReceiver() {
...
@@ -226,6 +231,7 @@ class SmsReceiver : BroadcastReceiver() {
}
}
}
}
}
}
confirmCallBack
.
invoke
()
}
}
private
fun
isMessageFilteredOut
(
context
:
Context
,
body
:
String
):
Boolean
{
private
fun
isMessageFilteredOut
(
context
:
Context
,
body
:
String
):
Boolean
{
...
@@ -237,14 +243,18 @@ class SmsReceiver : BroadcastReceiver() {
...
@@ -237,14 +243,18 @@ class SmsReceiver : BroadcastReceiver() {
return
false
return
false
}
}
/**
private
fun
getIndexById
(
userList
:
List
<
Conversation
>,
id
:
Long
):
Int
{
* 统计未读消息数量
for
(
i
in
userList
.
indices
)
{
*
if
(
userList
[
i
].
threadId
===
id
)
{
* 存储值在 Mainactivity.kt 完成
return
i
// 找到匹配的ID,返回下标
* 此处只做获取数值操作
}
* 1.未获取到值则表明没有未读短信,直接+1显示
}
* 2.获取到值也要数量+1 显示
return
-
1
// 如果未找到,返回-1
*/
private
fun
badgeTotalNumber
(
context
:
Context
)
{
val
readResult
:
Int
=
Config
.
newInstance
(
context
).
badgeTotalNumber
SmsCountUtil
.
setBadgeNum
(
context
,
readResult
+
1
)
Log
.
d
(
TAG
,
"badgeTotalNumber readResult = $readResult "
)
}
}
}
}
...
...
app/src/main/kotlin/com/secspace/sms/util/SmsCountUtil.kt
0 → 100644
View file @
bc419e39
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
()
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment