Commit d808999d authored by leichao.gao's avatar leichao.gao

update

parent eea87918
...@@ -6,7 +6,6 @@ import android.content.Intent ...@@ -6,7 +6,6 @@ import android.content.Intent
import android.os.Bundle import android.os.Bundle
import android.text.TextUtils import android.text.TextUtils
import com.base.browserwhite.fcm.FCMManager import com.base.browserwhite.fcm.FCMManager
import com.base.browserwhite.fcm.NotificationPushUtil.sendNotificationWhere
import com.base.browserwhite.fcm.PackageBroadcastReceiver import com.base.browserwhite.fcm.PackageBroadcastReceiver
import com.base.browserwhite.fcm.PackageBroadcastReceiver.Companion.registerPackageBroadcastReceiver import com.base.browserwhite.fcm.PackageBroadcastReceiver.Companion.registerPackageBroadcastReceiver
import com.base.browserwhite.fcm.ScreenStatusReceiver import com.base.browserwhite.fcm.ScreenStatusReceiver
......
package com.base.browserwhite.fcm
import android.util.Log
import com.base.browserwhite.bean.ConstObject
import com.base.browserwhite.utils.AppPreferences
import com.base.browserwhite.utils.LogEx
import com.base.browserwhite.utils.TimeUtils
import com.base.browserwhite.utils.TimeUtils.formatTimeH
object NotificationHelp {
private var currentNotificationIdIndex = -1
fun getNotificationId(): Int {
var id = 0
NOTIFICATION_IDS.forEach {
val boolean = canPushNotification(it)
if (boolean) {
id = it
Log.d("glc","forEach"+it)
return id
}
}
return id
}
/**
* 是否可以推送判断
*/
fun canPushNotification(actionId: Int): Boolean {
//默认可以推送
var canPush: Boolean = true
when (actionId) {
ConstObject.ID_JUNK_CLEANER -> {
canPush = canPushJunkClean()
}
ConstObject.ID_CLEAN_JUNK_MEMORY -> {
}
ConstObject.ID_NEWS -> {
canPush = canPushNews()
}
ConstObject.ID_WEATHER -> {
canPush = canPushWeather()
}
ConstObject.ID_APP_PROCESS -> {
canPush = canPushAppProcess()
}
}
return canPush
}
//天气
private fun canPushWeather(): Boolean {
val lastPushTime = AppPreferences.getInstance().getLong("last_notification_time", 0)
return lastPushTime == 0L || System.currentTimeMillis() - lastPushTime > 3 * 60 * 60 * 1000
}
//清理
private fun canPushJunkClean(): Boolean {
//last push
val lastPushTime = AppPreferences.getInstance().getLong("last_junk_notification_time", 0)
val flag1 =
lastPushTime == 0L || System.currentTimeMillis() - lastPushTime > 24 * 60 * 60 * 1000
//last use
val lastUseJunkTime = AppPreferences.getInstance().getLong("last_use_junk_clean", 0)
val flag2 =
lastUseJunkTime == 0L || System.currentTimeMillis() - lastUseJunkTime > 24 * 60 * 60 * 1000
return flag1 || flag2
}
//新闻
private fun canPushNews(): Boolean {
val time = System.currentTimeMillis().formatTimeH()
val lashPushTime =
AppPreferences.getInstance().getLong("last_news_notification_time", 0).formatTimeH()
return TimeUtils.isTimeBetweenEightAndTenPM() && time != lashPushTime
}
//app进程
private fun canPushAppProcess(): Boolean {
val lastPushTime =
AppPreferences.getInstance().getLong("last_process_notification_time", 0)
val flag1 =
lastPushTime == 0L || System.currentTimeMillis() - lastPushTime > 24 * 60 * 60 * 1000
val lastUseTime = AppPreferences.getInstance().getLong("last_process_use_time", 0)
val flag2 =
lastUseTime == 0L || System.currentTimeMillis() - lastUseTime > 24 * 60 * 60 * 1000
return flag1 || flag2
}
private fun getNextNotificationId(): Int {
// 将当前通知 ID 索引加 1
currentNotificationIdIndex++
// 如果当前通知 ID 索引超出列表范围,则将其重置为 0
if (currentNotificationIdIndex >= NOTIFICATION_IDS.size) {
currentNotificationIdIndex = 0
}
// 返回下一个通知 ID
return NOTIFICATION_IDS[currentNotificationIdIndex]
}
private val NOTIFICATION_IDS = intArrayOf(
ConstObject.ID_JUNK_CLEANER,
ConstObject.ID_CLEAN_JUNK_MEMORY,
ConstObject.ID_NEWS,
ConstObject.ID_APP_PROCESS,
ConstObject.ID_WEATHER
)
}
...@@ -23,170 +23,29 @@ import java.util.Locale ...@@ -23,170 +23,29 @@ import java.util.Locale
object NotificationPushUtil { object NotificationPushUtil {
private val TAG = "NotificationPushUtil" private val TAG = "NotificationPushUtil"
private var handlerThread: HandlerThread? = null
private var handler: Handler? = null
const val PUSH_WHERE_TIMBER = "push_where_timber"//定时推送 const val PUSH_WHERE_TIMBER = "push_where_timber"//定时推送
const val PUSH_WHERE_UN_INSTALL_BROADCAST = "push_where_un_install_broadcast"//安装卸载广播 const val PUSH_WHERE_UN_INSTALL_BROADCAST = "push_where_un_install_broadcast"//安装卸载广播
const val PUSH_WHERE_UNLOCK = "push_where_unlock"//解锁推送 const val PUSH_WHERE_UNLOCK = "push_where_unlock"//解锁推送
const val PUSH_WHERE_FCM = "push_where_fcm"//fcm推送 const val PUSH_WHERE_FCM = "push_where_fcm"//fcm推送
fun stopNotificationHandler() {
// 停止 HandlerThread
if (handler != null) {
handler?.removeCallbacksAndMessages(null)
}
if (handlerThread != null) {
handlerThread?.quit()
handlerThread = null
}
handler = null
}
/** /**
* 统一调用入口 * 统一调用入口
* @param where [PUSH_WHERE_TIMBER] * @param where [PUSH_WHERE_TIMBER]
*/ */
fun Context.sendNotificationWhere(setActionId: Int?, where: String) { fun Context.sendNotificationWhere(setActionId: Int?, where: String) {
val actionId: Int = setActionId ?: getNextNotificationId() Log.d("glc","sendNotificationWhere")
val actionId: Int = setActionId ?: NotificationHelp.getNotificationId()
if (MyApplication.PAUSED_VALUE == 1) { if (MyApplication.PAUSED_VALUE == 1) {
return return
} }
NotificationUiUtil.sendNotification(this, actionId)
NotificationUiUtil.sendNotification(this, actionId, where)
EventUtils.event("showNotification", where, null, false) EventUtils.event("showNotification", where, null, false)
// AppPreferences.getInstance().putInt("notificationCount_${getCurrentDate()}", todayShowCount + 1)
}
/**
* 是否可以推送判断
*/
fun canPushNotification(actionId: Int, where: String): Boolean {
//默认可以推送
var canPush: Boolean = true
// val maxShowNotificationCount = AppPreferences.getInstance().getString("maxShowNotificationCount", "200").toInt()
// val todayShowCount = AppPreferences.getInstance().getInt("notificationCount_${getCurrentDate()}", 0)
// if (todayShowCount >= maxShowNotificationCount) {
// return
// }
// val interval: Int = AppPreferences.getInstance().getString("notificationInterval", "60").toIntOrNull() ?: 60
// val lastTime: Long = AppPreferences.getInstance().getLong("last_notification_time", 0)
// val nowTime = System.currentTimeMillis()
// val x = nowTime - lastTime
// if (x < (interval * 1000)) {
// return
// }
when (actionId) {
ID_JUNK_CLEANER -> {
canPush = canPushJunkClean(where)
}
ID_CLEAN_JUNK_MEMORY -> {
}
ID_NEWS -> {
canPush = canPushNews(where)
LogEx.logDebug(TAG, "ID_NEWS canPush=$canPush")
}
ID_WEATHER -> {
canPush = canPushWeather(where)
LogEx.logDebug(TAG, "ID_WEATHER canPush=$canPush")
}
ID_APP_PROCESS -> {
canPush = canPushAppProcess(where)
LogEx.logDebug(TAG, "ID_APP_PROCESS canPush=$canPush")
}
}
return canPush
} }
//天气
private fun canPushWeather(where: String): Boolean {
val lastPushTime = AppPreferences.getInstance().getLong("last_notification_time", 0)
if (where == PUSH_WHERE_TIMBER || where == PUSH_WHERE_UNLOCK) {
return lastPushTime == 0L || System.currentTimeMillis() - lastPushTime > 3 * 60 * 60 * 1000
} else {
return false
}
}
//清理
private fun canPushJunkClean(where: String): Boolean {
if (where == PUSH_WHERE_TIMBER || where == PUSH_WHERE_UNLOCK) {
//last push
val lastPushTime = AppPreferences.getInstance().getLong("last_junk_notification_time", 0)
val flag1 = lastPushTime == 0L || System.currentTimeMillis() - lastPushTime > 24 * 60 * 60 * 1000
//last use
val lastUseJunkTime = AppPreferences.getInstance().getLong("last_use_junk_clean", 0)
val flag2 = lastUseJunkTime == 0L || System.currentTimeMillis() - lastUseJunkTime > 24 * 60 * 60 * 1000
return flag1 || flag2
} else {
return false
}
}
//新闻
private fun canPushNews(where: String): Boolean {
return true
if (where == PUSH_WHERE_TIMBER || where == PUSH_WHERE_UNLOCK) {
val time = System.currentTimeMillis().formatTimeH()
val lashPushTime = AppPreferences.getInstance().getLong("last_news_notification_time", 0).formatTimeH()
return isTimeBetweenEightAndTenPM() && time != lashPushTime
} else {
return false
}
}
//app进程
private fun canPushAppProcess(where: String): Boolean {
if (where == PUSH_WHERE_TIMBER || where == PUSH_WHERE_UNLOCK) {
val lastPushTime = AppPreferences.getInstance().getLong("last_process_notification_time", 0)
val flag1 = lastPushTime == 0L || System.currentTimeMillis() - lastPushTime > 24 * 60 * 60 * 1000
val lastUseTime = AppPreferences.getInstance().getLong("last_process_use_time", 0)
val flag2 = lastUseTime == 0L || System.currentTimeMillis() - lastUseTime > 24 * 60 * 60 * 1000
return flag1 || flag2
} else {
return false
}
}
private var currentNotificationIdIndex = -1
private fun getNextNotificationId(): Int {
// 将当前通知 ID 索引加 1
currentNotificationIdIndex++
// 如果当前通知 ID 索引超出列表范围,则将其重置为 0
if (currentNotificationIdIndex >= NOTIFICATION_IDS.size) {
currentNotificationIdIndex = 0
}
// 返回下一个通知 ID
return NOTIFICATION_IDS[currentNotificationIdIndex]
}
private val NOTIFICATION_IDS = intArrayOf(
ConstObject.ID_JUNK_CLEANER,
ConstObject.ID_CLEAN_JUNK_MEMORY,
ConstObject.ID_NEWS,
ConstObject.ID_APP_PROCESS,
ConstObject.ID_WEATHER
)
private fun getCurrentDate(): String { private fun getCurrentDate(): String {
val dateFormat = SimpleDateFormat("yyyy-MM-dd", Locale.getDefault()) val dateFormat = SimpleDateFormat("yyyy-MM-dd", Locale.getDefault())
......
...@@ -50,13 +50,7 @@ object NotificationUiUtil { ...@@ -50,13 +50,7 @@ object NotificationUiUtil {
private const val CHANNEL_NAME = "browser_fcm_channel" // 通知渠道名称 private const val CHANNEL_NAME = "browser_fcm_channel" // 通知渠道名称
@SuppressLint("RemoteViewLayout") @SuppressLint("RemoteViewLayout")
fun sendNotification(context: Context, actionId: Int, where: String) { fun sendNotification(context: Context, actionId: Int) {
if (!NotificationPushUtil.canPushNotification(actionId, where)) {
return
}
when (actionId) { when (actionId) {
ID_JUNK_CLEANER, ID_INSTALL_APP, ID_UNINSTALL_APP -> {//清理 ID_JUNK_CLEANER, ID_INSTALL_APP, ID_UNINSTALL_APP -> {//清理
var size = fastGetJunkSize(context) var size = fastGetJunkSize(context)
......
...@@ -68,8 +68,6 @@ class Splash2Activity : BaseActivity<ActivitySplash2Binding>(), ...@@ -68,8 +68,6 @@ class Splash2Activity : BaseActivity<ActivitySplash2Binding>(),
return return
} }
NotificationPushUtil.stopNotificationHandler()
NotificationPushUtil.stopNotificationHandlerNews()
jumpType = intent.getIntExtra("actionId", 0) jumpType = intent.getIntExtra("actionId", 0)
closeNotification() closeNotification()
mTaskManager = TaskManager(binding, this) mTaskManager = TaskManager(binding, this)
......
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