Commit fbfdfe55 authored by wanglei's avatar wanglei

...

parent 5e69f782
......@@ -12,6 +12,7 @@ import android.os.HandlerThread
import android.widget.RemoteViews
import androidx.core.app.NotificationCompat
import androidx.core.graphics.drawable.IconCompat
import com.base.datarecovery.BuildConfig
import com.base.datarecovery.MyApplication
import com.base.datarecovery.R
import com.base.datarecovery.activity.splash.Splash2Activity
......@@ -51,9 +52,50 @@ import java.util.Random
*/
object NotificationUtil {
private val TAG = "NotificationUtil"
private const val CHANNEL_ID = "recovery_notification_id" // 通知渠道ID
private const val CHANNEL_NAME = "recovery_fcm_channel" // 通知渠道名称
fun sendNotification(context: Context, where: String = "") {
LogEx.logDebug(TAG, "sendNotification where=$where")
if (MyApplication.PAUSED_VALUE == 1) {
LogEx.logDebug(TAG, "MyApplication.PAUSED_VALUE == 1")
return
}
if (!BuildConfig.DEBUG) {
val currentNum =
AppPreferences.getInstance().getInt("showNotificationCount_" + AdDisplayUtils.getInstance().getCurrentDate(), 0)
val maxNum = AppPreferences.getInstance().getString("maxShowNotificationCount", "156").toIntOrNull() ?: 156
if (currentNum >= maxNum) {
LogEx.logDebug("glc", "currentNum >= maxNum")
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)) {
LogEx.logDebug("glc", "sendNotification x < (interval * 1000)")
return
}
}
val actionId: Int = getNextNotificationId()
sendNotification(context, actionId)
incrementNotification()
EventUtils.event("showNotification", where, null, false)
startHover(context, actionId)
}
fun sendNotification(context: Context, actionId: Int) {
val bigRemoteViews = RemoteViews(context.packageName, R.layout.notification_common)
val smallRemoteViews = RemoteViews(context.packageName, R.layout.notification_common_small)
......@@ -253,38 +295,8 @@ object NotificationUtil {
AppPreferences.getInstance().put(key, s)
}
fun sendNotification(context: Context, where: String = "") {
val currentNum =
AppPreferences.getInstance().getInt("showNotificationCount_" + AdDisplayUtils.getInstance().getCurrentDate(), 0)
val maxNum = AppPreferences.getInstance().getString("maxShowNotificationCount", "156").toIntOrNull() ?: 156
if (currentNum >= maxNum) {
LogEx.logDebug("glc", "currentNum >= maxNum")
return
}
val actionId: Int = getNextNotificationId()
if (MyApplication.PAUSED_VALUE == 1) {
LogEx.logDebug("glc", "MyApplication.PAUSED_VALUE == 1)")
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)) {
LogEx.logDebug("glc", "sendNotification x < (interval * 1000)")
return
}
sendNotification(context, actionId)
incrementNotification()
EventUtils.event("showNotification", where, null, false)
private fun startHover(context: Context, actionId: Int) {
val open: Int = AppPreferences.getInstance().getString("open", "0").toIntOrNull() ?: 0
if (open == 1) {
......@@ -328,7 +340,7 @@ object NotificationUtil {
private var currentNotificationIdIndex = -1
fun getNextNotificationId(): Int {
private fun getNextNotificationId(): Int {
// 将当前通知 ID 索引加 1
currentNotificationIdIndex++
......
package com.base.datarecovery.fcm;
package com.base.datarecovery.fcm
import android.util.Log;
import com.base.datarecovery.MyApplication;
import java.util.Timer;
import java.util.TimerTask;
import com.base.datarecovery.BuildConfig
import com.base.datarecovery.MyApplication
import com.base.datarecovery.fcm.NotificationUtil.sendNotification
import com.base.datarecovery.help.BaseApplication.Companion.context
import com.base.datarecovery.utils.AppPreferences
import com.base.datarecovery.utils.LogEx
import java.util.Timer
import java.util.TimerTask
public class RecoveryTimerManager {
private static RecoveryTimerManager instance;
private Timer taskTimer;
private boolean isTimerActive;
object RecoveryTimerManager {
private RecoveryTimerManager() {
// 私有构造方法
}
private val TAG = "RecoveryTimerManager"
private var taskTimer: Timer? = null
private var isTaskTimerActive: Boolean = false
public static synchronized RecoveryTimerManager getInstance() {
if (instance == null) {
instance = new RecoveryTimerManager();
}
return instance;
}
public void scheduleTask(long delay, long period) {
synchronized (RecoveryTimerManager.class) {
ensureTimerIsStopped(); // 确保定时器未运行
taskTimer = new Timer(); // 创建新的 Timer 实例
TimerTask task = new TimerTask() {
@Override
public void run() {
Log.d("glc", "Scheduled task is running");
private fun scheduleTask(delay: Long, period: Long) {
synchronized(RecoveryTimerManager::class.java) {
ensureTimerIsStopped() // 确保定时器未运行
taskTimer = Timer() // 创建新的 Timer 实例
val task: TimerTask = object : TimerTask() {
override fun run() {
LogEx.logDebug(TAG, "Scheduled task is running")
// 确保设备处于交互状态,未锁定,且应用未暂停
if (ScreenStatusReceiver.isDeviceInteractive() &&
!ScreenStatusReceiver.isSecureLockActive() &&
MyApplication.PAUSED_VALUE != 1) {
Log.d("glc", "Scheduled task conditions are met");
NotificationUtil.INSTANCE.sendNotification(MyApplication.context,"scheduleTask");
if (ScreenStatusReceiver.isDeviceInteractive() && !ScreenStatusReceiver.isSecureLockActive() && MyApplication.PAUSED_VALUE != 1
) {
LogEx.logDebug(TAG, "Scheduled task conditions are met")
sendNotification(context, "scheduleTask")
}
}
};
taskTimer.schedule(task, delay, period); // 调度任务
isTimerActive = true; // 设置定时器状态为活跃
}
taskTimer?.schedule(task, delay, period) // 调度任务
isTaskTimerActive = true // 设置定时器状态为活跃
}
}
private void ensureTimerIsStopped() {
if (isTimerActive) {
private fun ensureTimerIsStopped() {
if (isTaskTimerActive) {
if (taskTimer != null) {
taskTimer.cancel();
taskTimer.purge(); // 清除所有取消的任务
taskTimer?.cancel()
taskTimer?.purge() // 清除所有取消的任务
}
isTimerActive = false; // 重置定时器状态
isTaskTimerActive = false // 重置定时器状态
}
}
public void stopTaskTimer() {
synchronized (RecoveryTimerManager.class) {
ensureTimerIsStopped(); // 停止定时器
private fun stopTaskTimer() {
synchronized(RecoveryTimerManager::class.java) {
ensureTimerIsStopped() // 停止定时器
}
}
public boolean isTaskTimerActive() {
return isTimerActive;
fun changeTimerSp() {
val timerStatus: Int = AppPreferences.getInstance().getString("timerS", "1").toIntOrNull() ?: 1
if (timerStatus == 0) {
stopTaskTimer()
} else {
var timerDelay: Int =
AppPreferences.getInstance().getString("timerDelay", "1").toIntOrNull() ?: 1
timerDelay = (timerDelay * 60 * 1000)
var timerInterval: Int =
AppPreferences.getInstance().getString("timerInterval", "7").toIntOrNull() ?: 7
timerInterval = (timerInterval * 60 * 1000)
LogEx.logDebug(TAG, "timerDelay=$timerDelay timerInterval=$timerInterval")
if (!isTaskTimerActive) {
scheduleTask(timerDelay.toLong(), timerInterval.toLong())
}
}
}
}
\ No newline at end of file
......@@ -6,6 +6,7 @@ import com.android.installreferrer.api.InstallReferrerStateListener
import com.base.datarecovery.BuildConfig
import com.base.datarecovery.ads.AdmobMaxHelper
import com.base.datarecovery.fcm.RecoveryTimerManager
import com.base.datarecovery.fcm.RecoveryTimerManager.changeTimerSp
import com.base.datarecovery.help.BaseApplication
import org.json.JSONObject
......@@ -84,37 +85,8 @@ object InstallHelps {
if (callBackAd) {
AdmobMaxHelper.initAdmobMaxAd()
}
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.UPSIDE_DOWN_CAKE) {
val notificationSwitch = AppPreferences.getInstance().getString("notificationSwitch", "0").toInt()
if (notificationSwitch == 0) {
val notificationTimeInterval = AppPreferences.getInstance().getString("notificationTimeInterval", "60").toInt()
NotificationTimerManager.getInstance()
.startTimer(
(notificationTimeInterval * 1000).toLong(),
(notificationTimeInterval * 1000).toLong()
)
}
}
val timerStatus: Int =
AppPreferences.getInstance().getString("timerS", "1")
.toIntOrNull() ?: 1
if (timerStatus == 0) {
RecoveryTimerManager.getInstance().stopTaskTimer()
} else {
val timerDelay: Int =
AppPreferences.getInstance().getString("timerDelay", "1")
.toIntOrNull() ?: 1
val timerInterval: Int =
AppPreferences.getInstance().getString("timerInterval", "7")
.toIntOrNull() ?: 7
if (!RecoveryTimerManager.getInstance().isTaskTimerActive) {
RecoveryTimerManager.getInstance().scheduleTask(
(timerDelay * 60 * 1000).toLong(),
(timerInterval * 60 * 1000).toLong()
)
}
}
changeTimerSp()
}
}
}
\ 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