-
Notifications
You must be signed in to change notification settings - Fork 30
/
RUnReadCount.kt
46 lines (39 loc) · 1.31 KB
/
RUnReadCount.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
@file:JvmName("RichUtils")
@file:JvmMultifileClass
package pyxis.uzuki.live.richutilskt.utils
import android.content.Context
import android.content.Intent
/**
* apply count of UnRead
*
* @param[count] count of setting count
*/
fun Context.applyUnReadCount(count: Int) {
val launcherClassName = getLauncherClassName(this)
if (launcherClassName.isEmpty()) return
applyDefault(this, count, launcherClassName)
}
/**
* remove count of UnRead
*/
fun Context.removeUnReadCount() {
this.applyUnReadCount(0)
}
private fun applyDefault(context: Context, count: Int, launcherClassName: String) {
val intent = Intent("android.intent.action.BADGE_COUNT_UPDATE")
intent.putExtra("badge_count_package_name", context.packageName)
intent.putExtra("badge_count_class_name", launcherClassName)
intent.putExtra("badge_count", count)
intent.flags = 0x00000020
context.sendBroadcast(intent)
}
private fun getLauncherClassName(context: Context): String {
val intent = Intent(Intent.ACTION_MAIN)
intent.addCategory(Intent.CATEGORY_LAUNCHER)
intent.`package` = context.packageName
val resolveInfoList = context.packageManager.queryIntentActivities(intent, 0)
if (resolveInfoList != null && resolveInfoList.size > 0) {
return resolveInfoList[0].activityInfo.name
}
return ""
}