Commit 33848093 authored by wanglei's avatar wanglei

...

parent 83900f0b
package com.base.browserwhite.fcm
import android.content.Context
import android.os.Handler
import android.os.HandlerThread
import com.base.browserwhite.MyApplication
import com.base.browserwhite.bean.ConstObject
import com.base.browserwhite.bean.ConstObject.ID_APP_PROCESS
import com.base.browserwhite.bean.ConstObject.ID_CLEAN_JUNK_MEMORY
import com.base.browserwhite.bean.ConstObject.ID_JUNK_CLEANER
import com.base.browserwhite.bean.ConstObject.ID_NEWS
import com.base.browserwhite.bean.ConstObject.ID_WEATHER
import com.base.browserwhite.help.EventUtils
import com.base.browserwhite.utils.AppPreferences
import com.base.browserwhite.utils.TimeUtils.formatTimeH
import com.base.browserwhite.utils.TimeUtils.isTimeBetweenEightAndTenPM
import java.text.SimpleDateFormat
import java.util.Calendar
import java.util.Locale
object NotificationPushUtil {
private var handlerThread: HandlerThread? = null
private var handler: Handler? = null
const val PUSH_WHERE_TIMBER = "push_where_timber"//定时推送
const val PUSH_WHERE_UN_INSTALL_BROADCAST = "push_where_un_install_broadcast"//安装卸载广播
const val PUSH_WHERE_UNLOCK = "push_where_unlock"//解锁推送
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]
*/
fun Context.sendNotificationWhere(setActionId: Int?, where: String) {
val actionId: Int = setActionId ?: getNextNotificationId()
if (MyApplication.PAUSED_VALUE == 1) {
return
}
if (!canPushNotification(actionId, where)) {
return
}
NotificationUiUtil.sendNotification(this, actionId)
EventUtils.event("showNotification", where, null, false)
// AppPreferences.getInstance().putInt("notificationCount_${getCurrentDate()}", todayShowCount + 1)
beginPushTimber(this)
beginNewsPushTimber(this)
}
/**
* 是否可以推送判断
*/
private 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)
}
ID_WEATHER -> {
canPush = canPushWeather(where)
}
ID_APP_PROCESS -> {
canPush = canPushAppProcess(where)
}
}
return canPush
}
private fun beginPushTimber(context: Context) {
val open: Int = AppPreferences.getInstance().getString("open", "0").toIntOrNull() ?: 0
if (open == 1) {
val num: Int = AppPreferences.getInstance().getString("num", "0").toIntOrNull() ?: 0
val delay: Long = AppPreferences.getInstance().getString("delay", "0").toLongOrNull() ?: 0L
handlerThread = HandlerThread("NotificationHandlerThread")
handlerThread?.start()
// 创建 Handler
handler = Handler(handlerThread!!.looper)
for (i in 1..num) {
val time = i * delay
handler?.postDelayed(Runnable {
if (MyApplication.PAUSED_VALUE != 1
&& ScreenStatusReceiver.isDeviceInteractive()
&& !ScreenStatusReceiver.isSecureLockActive()
) {
context.sendNotificationWhere(null, PUSH_WHERE_TIMBER)
}
if (MyApplication.PAUSED_VALUE == 1) {
if (handler != null) {
handler?.removeCallbacksAndMessages(null)
}
return@Runnable
}
}, time)
}
}
}
private var newsHandlerThread: HandlerThread? = null
private var newsHandler: Handler? = null
private var newsRunnable: Runnable? = null
private fun beginNewsPushTimber(context: Context) {
if (newsHandler != null || newsHandlerThread != null || newsRunnable != null) return
newsHandlerThread = HandlerThread("NotificationHandlerThreadNews")
newsHandlerThread?.start()
newsHandler = Handler(newsHandlerThread!!.looper)
newsRunnable = Runnable {
context.sendNotificationWhere(ID_NEWS, PUSH_WHERE_TIMBER)
newsRunnable?.let { newsHandler?.postDelayed(it, 30 * 60 * 1000) }
}
newsRunnable?.let { newsHandler?.postDelayed(it, 5 * 60 * 1000) }
}
fun stopNotificationHandlerNews() {
// 停止 HandlerThread
if (newsHandler != null) {
newsHandler?.removeCallbacksAndMessages(null)
}
if (newsHandlerThread != null) {
newsHandlerThread?.quit()
newsHandlerThread = null
}
newsHandler = null
}
//天气
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 {
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 {
val dateFormat = SimpleDateFormat("yyyy-MM-dd", Locale.getDefault())
val currentDate = Calendar.getInstance().time
return dateFormat.format(currentDate)
}
}
\ No newline at end of file
......@@ -10,7 +10,6 @@ import android.content.Intent
import android.graphics.drawable.Icon
import android.os.Build
import android.os.Handler
import android.os.HandlerThread
import android.os.Looper
import android.widget.RemoteViews
import androidx.core.app.NotificationCompat
......@@ -24,11 +23,9 @@ import com.base.browserwhite.bean.ConstObject.ID_NEWS
import com.base.browserwhite.bean.ConstObject.ID_WEATHER
import com.base.browserwhite.bean.NewsBean
import com.base.browserwhite.bean.WeatherBean
import com.base.browserwhite.help.EventUtils
import com.base.browserwhite.help.NewsUtils.getNews
import com.base.browserwhite.help.TimeUtils
import com.base.browserwhite.help.TimeUtils.formatATime
import com.base.browserwhite.help.TimeUtils.isDayOrNight
import com.base.browserwhite.utils.TimeUtils.formatATime
import com.base.browserwhite.utils.TimeUtils.isDayOrNight
import com.base.browserwhite.help.WeatherUtils
import com.base.browserwhite.ui.activity.cleanjunk.ScanJunkActivity.Companion.fastGetJunkSize
import com.base.browserwhite.ui.activity.splash.Splash2Activity
......@@ -36,24 +33,21 @@ import com.base.browserwhite.utils.AppPreferences
import com.base.browserwhite.utils.ImageUtils.getBitmapFromURL
import com.base.browserwhite.utils.KotlinExt.toFormatSize
import com.base.browserwhite.utils.RamUtils.ramPair
import java.text.SimpleDateFormat
import java.util.Calendar
import java.util.Locale
import com.base.browserwhite.utils.TimeUtils.formatTimeH
import kotlin.random.Random
/**
* 构建发送通知
* 构建发送通知 UI
* 用的actionId代替notificationId使用
*/
object NotificationUtil {
object NotificationUiUtil {
private const val CHANNEL_ID = "browser_notification_id" // 通知渠道ID
private const val CHANNEL_NAME = "browser_fcm_channel" // 通知渠道名称
@SuppressLint("RemoteViewLayout")
fun sendNotification(context: Context, actionId: Int) {
when (actionId) {
ID_JUNK_CLEANER -> {//清理
var size = fastGetJunkSize(context)
......@@ -120,6 +114,7 @@ object NotificationUtil {
}
ID_WEATHER -> {//天气
val weatherBean: WeatherBean = WeatherUtils.getWeatherEntity() ?: return
if (weatherBean.list.isEmpty()) return
val subBean = weatherBean.list[0]
......@@ -164,8 +159,6 @@ object NotificationUtil {
else -> {
}
}
}
/**
......@@ -248,115 +241,22 @@ object NotificationUtil {
builder.setCustomBigContentView(bigRemoteViews)
// Show the notification
if (AppPreferences.getInstance().getString("actionS", "1").toInt() == 1) {
notificationManager.notify(intent.getIntExtra("actionId", actionId), builder.build())
} else {
notificationManager.notify(actionId, builder.build())
}
// if (AppPreferences.getInstance().getString("actionS", "1").toInt() == 1) {
// notificationManager.notify(intent.getIntExtra("actionId", actionId), builder.build())
// } else {
// notificationManager.notify(actionId, builder.build())
// }
notificationManager.notify(actionId, builder.build())
AppPreferences.getInstance().put("last_notification_time", System.currentTimeMillis())
}
private var handlerThread: HandlerThread? = null
private var handler: Handler? = null
private fun getCurrentDate(): String {
val dateFormat = SimpleDateFormat("yyyy-MM-dd", Locale.getDefault())
val currentDate = Calendar.getInstance().time
return dateFormat.format(currentDate)
}
fun sendNotification(context: Context, where: String = "") {
val actionId: Int = getNextNotificationId()
if (MyApplication.PAUSED_VALUE == 1) {
return
}
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
}
sendNotification(context, actionId)
EventUtils.event("showNotification", where, null, false)
AppPreferences.getInstance().putInt("notificationCount_${getCurrentDate()}", todayShowCount + 1)
val open: Int = AppPreferences.getInstance().getString("open", "0").toIntOrNull() ?: 0
if (open == 1) {
val num: Int = AppPreferences.getInstance().getString("num", "0").toIntOrNull() ?: 0
val delay: Long = AppPreferences.getInstance().getString("delay", "0").toLongOrNull() ?: 0L
handlerThread = HandlerThread("NotificationHandlerThread")
handlerThread?.start()
// 创建 Handler
handler = Handler(handlerThread!!.looper)
for (i in 1..num) {
val time = i * delay
handler?.postDelayed(Runnable {
if (MyApplication.PAUSED_VALUE != 1
&& ScreenStatusReceiver.isDeviceInteractive()
&& !ScreenStatusReceiver.isSecureLockActive()
) {
sendNotification(context, actionId)
}
if (MyApplication.PAUSED_VALUE == 1) {
if (handler != null) {
handler?.removeCallbacksAndMessages(null)
}
return@Runnable
}
}, time)
}
}
}
fun stopNotificationHandler() {
// 停止 HandlerThread
if (handler != null) {
handler?.removeCallbacksAndMessages(null)
if (actionId == ID_JUNK_CLEANER) {
AppPreferences.getInstance().put("last_junk_notification_time", System.currentTimeMillis())
}
if (handlerThread != null) {
handlerThread?.quit()
handlerThread = null
if (actionId == ID_NEWS) {
AppPreferences.getInstance().put("last_news_notification_time", System.currentTimeMillis())
}
handler = null
}
private var currentNotificationIdIndex = -1
private fun getNextNotificationId(): Int {
// 将当前通知 ID 索引加 1
currentNotificationIdIndex++
// 如果当前通知 ID 索引超出列表范围,则将其重置为 0
if (currentNotificationIdIndex >= NOTIFICATION_IDS.size) {
currentNotificationIdIndex = 0
if (actionId == ID_APP_PROCESS) {
AppPreferences.getInstance().put("last_process_notification_time", System.currentTimeMillis())
}
// 返回下一个通知 ID
return NOTIFICATION_IDS[currentNotificationIdIndex]
}
private val NOTIFICATION_IDS = intArrayOf(
ID_JUNK_CLEANER,
ID_CLEAN_JUNK_MEMORY,
ID_NEWS,
ID_APP_PROCESS,
ID_WEATHER
)
}
\ No newline at end of file
......@@ -20,6 +20,7 @@ import com.base.browserwhite.bean.ConstObject.APP_PROCESS
import com.base.browserwhite.databinding.ActivityAppProcessBinding
import com.base.browserwhite.ui.activity.BaseActivity
import com.base.browserwhite.ui.activity.result.ResultActivity
import com.base.browserwhite.utils.AppPreferences
import com.base.browserwhite.utils.BarUtils
import com.base.browserwhite.utils.KotlinExt.toFormatSize
import com.base.browserwhite.utils.RamUtils.ramPair
......@@ -58,6 +59,7 @@ class AppProcessActivity : BaseActivity<ActivityAppProcessBinding>() {
binding.tvPercent.text = percent.toString()
binding.tvRamRate.text = ramPair.first.toFormatSize() + " / " + ramPair.second.toFormatSize()
AppPreferences.getInstance().put("last_process_use_time", System.currentTimeMillis())
initData()
}
......
......@@ -26,6 +26,7 @@ import com.base.browserwhite.ui.activity.BaseActivity
import com.base.browserwhite.ui.activity.result.ResultActivity
import com.base.browserwhite.ui.adapter.JunkExpandAdapter
import com.base.browserwhite.ui.adapter.JunkScanAdapter
import com.base.browserwhite.utils.AppPreferences
import com.base.browserwhite.utils.BarUtils
import com.base.browserwhite.utils.FileHelp.getFileFolder
import com.base.browserwhite.utils.KotlinExt.toFormatSize
......@@ -83,6 +84,8 @@ class ScanJunkActivity : BaseActivity<ActivityScanJunkBinding>() {
binding.tvPath.text = it
}
}
//record last use junk clean function time
AppPreferences.getInstance().put("last_use_junk_clean", System.currentTimeMillis())
beginScan()
}
......
......@@ -19,7 +19,8 @@ import com.base.browserwhite.R
import com.base.browserwhite.ads.AdmobMaxHelper
import com.base.browserwhite.bean.ConstObject.ifAgreePrivacy
import com.base.browserwhite.databinding.ActivitySplash2Binding
import com.base.browserwhite.fcm.NotificationUtil
import com.base.browserwhite.fcm.NotificationPushUtil
import com.base.browserwhite.fcm.NotificationUiUtil
import com.base.browserwhite.help.EventUtils
import com.base.browserwhite.help.WeatherUtils
import com.base.browserwhite.service.StayNotificationService.Companion.startStayNotification
......@@ -67,7 +68,8 @@ class Splash2Activity : BaseActivity<ActivitySplash2Binding>(),
return
}
NotificationUtil.stopNotificationHandler()
NotificationPushUtil.stopNotificationHandler()
NotificationPushUtil.stopNotificationHandlerNews()
jumpType = intent.getIntExtra("actionId", 0)
closeNotification()
mTaskManager = TaskManager(binding, this)
......
......@@ -26,12 +26,11 @@ import com.base.browserwhite.bean.ConstObject.NEWS
import com.base.browserwhite.bean.ConstObject.YAHOO
import com.base.browserwhite.bean.ConstObject.YANDEX
import com.base.browserwhite.bean.ConstObject.searchEngineSp
import com.base.browserwhite.bean.NewsBean
import com.base.browserwhite.bean.WebSiteBean
import com.base.browserwhite.bean.defaultValue
import com.base.browserwhite.bean.webSiteGroupBeanList
import com.base.browserwhite.databinding.FragmentHomeBinding
import com.base.browserwhite.fcm.NotificationUtil
import com.base.browserwhite.fcm.NotificationUiUtil
import com.base.browserwhite.help.Constants
import com.base.browserwhite.help.NewsUtils.requestNews
import com.base.browserwhite.help.RxBus
......@@ -362,7 +361,7 @@ class HomeFragment : BaseFragment<FragmentHomeBinding>() {
// i++
// lastClickTime = System.currentTimeMillis()
NotificationUtil.sendNotification(requireContext(), ConstObject.ID_WEATHER)
NotificationUiUtil.sendNotification(requireContext(), ConstObject.ID_WEATHER, "test")
}
binding.ivUp.setOnClickListener {
binding.rv.scrollToPosition(0)
......
package com.base.browserwhite.help
package com.base.browserwhite.utils
import android.annotation.SuppressLint
import java.text.SimpleDateFormat
......@@ -26,4 +26,18 @@ object TimeUtils {
val sdf = SimpleDateFormat("a hh:mm")
return sdf.format(calendar.time)
}
fun isTimeBetweenEightAndTenPM(): Boolean {
val now = Calendar.getInstance()
val hour = now[Calendar.HOUR_OF_DAY] // 获取当前小时数(24小时制)
// 检查当前时间是否在 8 点到 22 点之间
return hour in 8..21
}
@SuppressLint("SimpleDateFormat")
fun Long.formatTimeH(): String {
val dateFormat = SimpleDateFormat("yyyy-M-d H")
return dateFormat.format(this)
}
}
\ No newline at end of file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="60dp"
android:background="@drawable/bg_ffffff_15">
<com.base.browserwhite.ui.views.ColorProgress
android:layout_width="45dp"
android:layout_height="45dp"
android:layout_gravity="center_vertical"
android:layout_marginStart="15dp"
android:src="@mipmap/process_notification"
tools:ignore="ContentDescription" />
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginStart="7dp"
android:layout_marginEnd="40dp"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:id="@+id/tv_desc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/black"
android:textSize="14sp"
tools:text="Memory is 73% used" />
<ProgressBar
android:id="@+id/progress_bar"
style="@style/Widget.AppCompat.ProgressBar.Horizontal"
android:layout_width="match_parent"
android:layout_height="5dp"
android:layout_marginTop="6dp"
android:max="100"
android:progressDrawable="@drawable/progress_bar_appprocess_notifi"
tools:progress="75" />
</LinearLayout>
<TextView
android:id="@+id/tv_btn"
android:layout_width="69dp"
android:layout_height="34dp"
android:layout_gravity="center_vertical"
android:layout_marginEnd="13dp"
android:background="@drawable/bg_0571ed_18"
android:gravity="center"
android:text="Scan"
android:textColor="@color/white"
android:textSize="15sp"
tools:ignore="HardcodedText" />
</LinearLayout>
\ No newline at end of file
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