Commit fa7c10dc by zhangchengbo

fix:修改first函数为firstOrNull,防止崩溃问题

parent 1f0911d0
......@@ -2,8 +2,16 @@ package com.secspace.sms.activities
import android.annotation.SuppressLint
import android.app.Activity
import android.app.Notification
import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.role.RoleManager
import android.content.Context
import android.content.Intent
import android.content.pm.PackageManager
import android.content.pm.ShortcutManager
import android.graphics.Color
import android.net.Uri
import android.os.Build
import android.os.Bundle
import android.provider.Telephony
......@@ -11,6 +19,7 @@ import android.text.TextUtils
import android.util.Log
import androidx.annotation.RequiresApi
import androidx.coordinatorlayout.widget.CoordinatorLayout
import androidx.core.app.NotificationCompat
import androidx.lifecycle.lifecycleScope
import com.secspace.sms.R
import com.secspace.sms.adapters.ConversationsAdapter
......@@ -33,8 +42,9 @@ import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode
class MainActivity : SimpleActivity() {
private val TAG = "shuju"
private val TAG = "MainActivity"
private val MAKE_DEFAULT_APP_REQUEST = 1
private var storedTextColor = 0
......@@ -77,6 +87,89 @@ class MainActivity : SimpleActivity() {
if (checkAppSideloading()) {
return
}
// testSendNotify()
// setBadgeNum(99)
// setBadgeCount(this@MainActivity,66)
}
//测试通知
private fun testSendNotify() {
val builder: NotificationCompat.Builder
//8.0
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
builder = NotificationCompat.Builder(this, getChannelId())
builder.setSmallIcon(com.secspace.sms.R.mipmap.ic_launcher)
.setDefaults(Notification.DEFAULT_ALL)
.setTicker("title")
.setAutoCancel(true)
.setContentTitle("contentTitle")
.setContentText("contentText")
.setNumber(99)
notificationManager.notify(123, builder.build())
}
}
/**
* https://blog.51cto.com/u_16213326/11294652
*/
private fun setBadgeCount(context: Context, count: Int) {
val intent = Intent("android.intent.action.BADGE_COUNT_UPDATE")
intent.putExtra("badge_count", count)
intent.putExtra("badge_count_package_name", context.packageName)
intent.putExtra("badge_count_class_name", getLauncherClassName(context))
context.sendBroadcast(intent)
}
private fun getLauncherClassName(context: Context): String? {
val pm = context.packageManager
val intent = Intent(Intent.ACTION_MAIN)
intent.addCategory(Intent.CATEGORY_LAUNCHER)
val resolveInfos = pm.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY)
for (resolveInfo in resolveInfos) {
val pkgName = resolveInfo.activityInfo.applicationInfo.packageName
if (pkgName.equals(context.packageName, ignoreCase = true)) {
return resolveInfo.activityInfo.name
}
}
return null
}
//https://developer.honor.com/cn/forum/topicdetail/topicid-3540765949296640
private fun setBadgeNum(num: Int) {
try {
val bunlde = Bundle()
bunlde.putString("package", "com.test.badge") // com.test.badge is your package name
bunlde.putString("class", "com.test. badge.MainActivity") // 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) {
// mIsSupportedBade = false
}
}
@RequiresApi(api = Build.VERSION_CODES.O)
fun getChannelId(): String {
val channelId = "1"
val channelName = "sms"
val channel = NotificationChannel(channelId, channelName, NotificationManager.IMPORTANCE_DEFAULT)
channel.enableLights(true) //显示桌面红点
channel.lightColor = Color.RED
channel.setShowBadge(true)
notificationManager.createNotificationChannel(channel)
return channel.id
}
@RequiresApi(Build.VERSION_CODES.N_MR1)
fun getShortcutCount(context: Context): Int {
val shortcutManager: ShortcutManager = context.getSystemService(ShortcutManager::class.java)
val shortcutInfoList = shortcutManager.dynamicShortcuts
return shortcutInfoList.size
}
override fun onResume() {
......@@ -416,13 +509,13 @@ class MainActivity : SimpleActivity() {
val phoneNumber = firstConversation.title.replace(" ", "")
//只针对手机号
if (phoneNumber.length != 11) return
if (phoneNumber.length != 11 ) return
if (!firstConversation.isSFNumber) {
Log.d(TAG, "----MainActivity----获取第一条会话手机号 phoneNumber:$phoneNumber")
RecentsHelper(context = this@MainActivity).getRecentCalls(false, Int.MAX_VALUE) { recents: List<RecentCall> ->
val listCallRecent: List<RecentCall> = recents.distinctBy { it.phoneNumber }
// Log.d(TAG, "----MainActivity----最近通话记录列表 :${GsonUtil.parseListToJson(listCallRecent)}")
val findResult: RecentCall = listCallRecent.first { it.phoneNumber.trim() == phoneNumber && it.isShunFeng }
val findResult: RecentCall? = listCallRecent.firstOrNull { it.phoneNumber.replace(" ","") == phoneNumber && it.isShunFeng }
Log.d(TAG, "----MainActivity----是否从通话记录中查找到电话号Result:${findResult != null}")
//从最近通话记录中找到了号码 进行掩码操作
if (findResult != null) {
......@@ -643,8 +736,8 @@ class MainActivity : SimpleActivity() {
@Subscribe(threadMode = ThreadMode.MAIN)
fun refreshMessages(event: Events.RefreshMessages) {
// Log.e(TAG, "----MainActivity----refreshMessages EventBus")
// initMessenger()
Log.e(TAG, "----MainActivity----refreshMessages EventBus")
initMessenger()
}
}
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