新增:自动任务·快捷指令 —— 电池电量&充电状态改变(废弃:`88888888`)
parent
e03a9b8198
commit
eb20d8ea05
@ -1,73 +0,0 @@
|
||||
package com.idormy.sms.forwarder.receiver
|
||||
|
||||
import android.content.BroadcastReceiver
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.util.Log
|
||||
import com.google.gson.Gson
|
||||
import com.idormy.sms.forwarder.App
|
||||
import com.idormy.sms.forwarder.database.AppDatabase
|
||||
import com.idormy.sms.forwarder.database.entity.Task
|
||||
import com.idormy.sms.forwarder.entity.task.CronSetting
|
||||
import com.idormy.sms.forwarder.entity.task.TaskSetting
|
||||
import com.idormy.sms.forwarder.utils.task.AlarmUtils
|
||||
import gatewayapps.crondroid.CronExpression
|
||||
import java.util.Date
|
||||
|
||||
@Suppress("PropertyName", "DEPRECATION")
|
||||
class AlarmReceiver : BroadcastReceiver() {
|
||||
|
||||
val TAG: String = AlarmReceiver::class.java.simpleName
|
||||
|
||||
override fun onReceive(context: Context, intent: Intent) {
|
||||
val task = intent.getParcelableExtra<Task>("TASK")
|
||||
if (task == null) {
|
||||
Log.d(TAG, "onReceive task is null")
|
||||
return
|
||||
}
|
||||
|
||||
Log.d(TAG, "onReceive task $task")
|
||||
Log.d(TAG, "lastExecTime = ${task.lastExecTime}, nextExecTime = ${task.nextExecTime}")
|
||||
try {
|
||||
//取消旧任务的定时器
|
||||
AlarmUtils.cancelAlarm(task)
|
||||
|
||||
// 根据任务信息执行相应操作
|
||||
val conditionList = Gson().fromJson(task.conditions, Array<TaskSetting>::class.java).toMutableList()
|
||||
if (conditionList.isEmpty()) {
|
||||
Log.d(TAG, "onReceive conditionList is empty")
|
||||
return
|
||||
}
|
||||
val firstCondition = conditionList.firstOrNull()
|
||||
if (firstCondition == null) {
|
||||
Log.d(TAG, "onReceive firstCondition is null")
|
||||
return
|
||||
}
|
||||
val cronSetting = Gson().fromJson(firstCondition.setting, CronSetting::class.java)
|
||||
if (cronSetting == null) {
|
||||
Log.d(TAG, "onReceive cronSetting is null")
|
||||
return
|
||||
}
|
||||
// 更新任务的上次执行时间和下次执行时间
|
||||
val cronExpression = CronExpression(cronSetting.expression)
|
||||
task.lastExecTime = Date()
|
||||
task.nextExecTime = cronExpression.getNextValidTimeAfter(task.lastExecTime)
|
||||
Log.d(TAG, "lastExecTime = ${task.lastExecTime}, nextExecTime = ${task.nextExecTime}")
|
||||
// 自动禁用任务
|
||||
if (task.nextExecTime <= task.lastExecTime) {
|
||||
task.status = 0
|
||||
}
|
||||
// 更新任务信息
|
||||
AppDatabase.getInstance(App.context).taskDao().update(task)
|
||||
if (task.status == 0) {
|
||||
Log.d(TAG, "onReceive task is disabled")
|
||||
return
|
||||
}
|
||||
//设置新的定时器
|
||||
AlarmUtils.scheduleAlarm(task)
|
||||
} catch (e: Exception) {
|
||||
Log.e(TAG, "onReceive error $e")
|
||||
}
|
||||
|
||||
}
|
||||
}
|
@ -0,0 +1,79 @@
|
||||
package com.idormy.sms.forwarder.receiver
|
||||
|
||||
import android.content.BroadcastReceiver
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.os.BatteryManager
|
||||
import android.util.Log
|
||||
import androidx.work.OneTimeWorkRequestBuilder
|
||||
import androidx.work.WorkManager
|
||||
import androidx.work.workDataOf
|
||||
import com.idormy.sms.forwarder.utils.BatteryUtils
|
||||
import com.idormy.sms.forwarder.utils.TASK_CONDITION_BATTERY
|
||||
import com.idormy.sms.forwarder.utils.TASK_CONDITION_CHARGE
|
||||
import com.idormy.sms.forwarder.utils.TaskWorker
|
||||
import com.idormy.sms.forwarder.utils.task.TaskUtils
|
||||
import com.idormy.sms.forwarder.workers.BatteryWorker
|
||||
|
||||
@Suppress("PropertyName")
|
||||
class BatteryReceiver : BroadcastReceiver() {
|
||||
|
||||
val TAG: String = BatteryReceiver::class.java.simpleName
|
||||
|
||||
override fun onReceive(context: Context?, intent: Intent?) {
|
||||
|
||||
if (context == null || intent?.action != Intent.ACTION_BATTERY_CHANGED) return
|
||||
|
||||
val batteryInfo = BatteryUtils.getBatteryInfo(intent).toString()
|
||||
TaskUtils.batteryInfo = batteryInfo
|
||||
|
||||
val levelNew = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, 0)
|
||||
val levelOld = TaskUtils.batteryLevel
|
||||
val isLevelChanged = levelNew != levelOld
|
||||
TaskUtils.batteryLevel = levelNew
|
||||
|
||||
val scale: Int = intent.getIntExtra(BatteryManager.EXTRA_SCALE, 100)
|
||||
TaskUtils.batteryPct = levelNew.toFloat() / scale.toFloat() * 100
|
||||
|
||||
val pluggedNew: Int = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1)
|
||||
val pluggedOld = TaskUtils.batteryPlugged
|
||||
val isPluggedChanged = pluggedNew != pluggedOld
|
||||
TaskUtils.batteryPlugged = pluggedNew
|
||||
|
||||
val statusNew: Int = intent.getIntExtra(BatteryManager.EXTRA_STATUS, -1)
|
||||
val statusOld = TaskUtils.batteryStatus
|
||||
val isStatusChanged = statusNew != statusOld
|
||||
TaskUtils.batteryStatus = statusNew
|
||||
|
||||
//电量改变
|
||||
if (isLevelChanged) {
|
||||
Log.d(TAG, "电量改变")
|
||||
val request = OneTimeWorkRequestBuilder<BatteryWorker>().setInputData(
|
||||
workDataOf(
|
||||
TaskWorker.conditionType to TASK_CONDITION_BATTERY,
|
||||
"status" to statusNew,
|
||||
"level_new" to levelNew,
|
||||
"level_old" to levelOld,
|
||||
)
|
||||
).build()
|
||||
WorkManager.getInstance(context).enqueue(request)
|
||||
}
|
||||
|
||||
//充电状态改变
|
||||
if (isPluggedChanged || isStatusChanged) {
|
||||
Log.d(TAG, "充电状态改变")
|
||||
val request = OneTimeWorkRequestBuilder<BatteryWorker>().setInputData(
|
||||
workDataOf(
|
||||
TaskWorker.conditionType to TASK_CONDITION_CHARGE,
|
||||
"status_new" to statusNew,
|
||||
"status_old" to statusOld,
|
||||
"plugged_new" to pluggedNew,
|
||||
"plugged_old" to pluggedOld,
|
||||
)
|
||||
).build()
|
||||
WorkManager.getInstance(context).enqueue(request)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,28 @@
|
||||
package com.idormy.sms.forwarder.receiver
|
||||
|
||||
import android.content.BroadcastReceiver
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.util.Log
|
||||
import com.idormy.sms.forwarder.activity.SplashActivity
|
||||
|
||||
@Suppress("PropertyName")
|
||||
class BootCompletedReceiver : BroadcastReceiver() {
|
||||
|
||||
val TAG: String = BootCompletedReceiver::class.java.simpleName
|
||||
|
||||
override fun onReceive(context: Context, intent: Intent?) {
|
||||
|
||||
if (intent?.action != Intent.ACTION_BOOT_COMPLETED && intent?.action != Intent.ACTION_LOCKED_BOOT_COMPLETED) return
|
||||
|
||||
try {
|
||||
Log.d(TAG, "强制重启APP一次")
|
||||
val intent1 = Intent(context, SplashActivity::class.java)
|
||||
intent1.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK)
|
||||
context.startActivity(intent1)
|
||||
} catch (e: Exception) {
|
||||
e.printStackTrace()
|
||||
}
|
||||
|
||||
}
|
||||
}
|
@ -1,28 +0,0 @@
|
||||
package com.idormy.sms.forwarder.receiver
|
||||
|
||||
import android.content.BroadcastReceiver
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.util.Log
|
||||
import com.idormy.sms.forwarder.activity.SplashActivity
|
||||
|
||||
@Suppress("PropertyName")
|
||||
class BootReceiver : BroadcastReceiver() {
|
||||
|
||||
val TAG: String = BootReceiver::class.java.simpleName
|
||||
|
||||
override fun onReceive(context: Context, intent: Intent?) {
|
||||
val receiveAction: String? = intent?.action
|
||||
Log.d(TAG, "onReceive intent $receiveAction")
|
||||
if (receiveAction == Intent.ACTION_BOOT_COMPLETED || receiveAction == Intent.ACTION_LOCKED_BOOT_COMPLETED) {
|
||||
try {
|
||||
Log.d(TAG, "强制重启APP一次")
|
||||
val intent1 = Intent(context, SplashActivity::class.java)
|
||||
intent1.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK)
|
||||
context.startActivity(intent1)
|
||||
} catch (e: Exception) {
|
||||
e.printStackTrace()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -1,172 +0,0 @@
|
||||
package com.idormy.sms.forwarder.service
|
||||
|
||||
import android.annotation.SuppressLint
|
||||
import android.app.Service
|
||||
import android.content.BroadcastReceiver
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.content.IntentFilter
|
||||
import android.os.IBinder
|
||||
import android.text.TextUtils
|
||||
import android.util.Log
|
||||
import androidx.work.OneTimeWorkRequestBuilder
|
||||
import androidx.work.WorkManager
|
||||
import androidx.work.workDataOf
|
||||
import com.google.gson.Gson
|
||||
import com.idormy.sms.forwarder.App
|
||||
import com.idormy.sms.forwarder.R
|
||||
import com.idormy.sms.forwarder.core.Core
|
||||
import com.idormy.sms.forwarder.database.AppDatabase
|
||||
import com.idormy.sms.forwarder.entity.MsgInfo
|
||||
import com.idormy.sms.forwarder.utils.BatteryUtils
|
||||
import com.idormy.sms.forwarder.utils.CacheUtils
|
||||
import com.idormy.sms.forwarder.utils.HistoryUtils
|
||||
import com.idormy.sms.forwarder.utils.SettingUtils
|
||||
import com.idormy.sms.forwarder.utils.Worker
|
||||
import com.idormy.sms.forwarder.workers.SendWorker
|
||||
import com.xuexiang.xutil.file.FileUtils
|
||||
import frpclib.Frpclib
|
||||
import kotlinx.coroutines.DelicateCoroutinesApi
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.GlobalScope
|
||||
import kotlinx.coroutines.async
|
||||
import java.util.*
|
||||
|
||||
@OptIn(DelicateCoroutinesApi::class)
|
||||
@Suppress("DeferredResultUnused")
|
||||
class BatteryService : Service() {
|
||||
|
||||
override fun onBind(intent: Intent): IBinder? {
|
||||
return null
|
||||
}
|
||||
|
||||
override fun onCreate() {
|
||||
super.onCreate()
|
||||
Log.i(TAG, "onCreate--------------")
|
||||
|
||||
//纯客户端模式
|
||||
//if (SettingUtils.enablePureClientMode) return
|
||||
|
||||
val batteryFilter = IntentFilter()
|
||||
batteryFilter.addAction(Intent.ACTION_BATTERY_CHANGED)
|
||||
registerReceiver(batteryReceiver, batteryFilter)
|
||||
}
|
||||
|
||||
override fun onStartCommand(intent: Intent, flags: Int, startId: Int): Int {
|
||||
Log.i(TAG, "onStartCommand--------------")
|
||||
return START_STICKY
|
||||
}
|
||||
|
||||
override fun onDestroy() {
|
||||
Log.i(TAG, "onDestroy--------------")
|
||||
super.onDestroy()
|
||||
|
||||
//纯客户端模式
|
||||
//if (SettingUtils.enablePureClientMode) return
|
||||
|
||||
unregisterReceiver(batteryReceiver)
|
||||
}
|
||||
|
||||
// 接收电池信息更新的广播
|
||||
private val batteryReceiver: BroadcastReceiver = object : BroadcastReceiver() {
|
||||
@SuppressLint("DefaultLocale")
|
||||
override fun onReceive(context: Context, intent: Intent) {
|
||||
|
||||
if (intent.action != Intent.ACTION_BATTERY_CHANGED) return
|
||||
|
||||
//自动删除N天前的转发记录
|
||||
if (SettingUtils.autoCleanLogsDays > 0) {
|
||||
Log.d(TAG, "自动删除N天前的转发记录")
|
||||
val cal = Calendar.getInstance()
|
||||
cal.add(Calendar.DAY_OF_MONTH, 0 - SettingUtils.autoCleanLogsDays)
|
||||
Core.msg.deleteTimeAgo(cal.timeInMillis)
|
||||
|
||||
//清理缓存
|
||||
HistoryUtils.clearPreference()
|
||||
CacheUtils.clearAllCache(context)
|
||||
}
|
||||
|
||||
//守护自启动的Frpc
|
||||
if (FileUtils.isFileExists(filesDir.absolutePath + "/libs/libgojni.so")) {
|
||||
GlobalScope.async(Dispatchers.IO) {
|
||||
val frpcList = AppDatabase.getInstance(App.context).frpcDao().getAutorun()
|
||||
|
||||
if (frpcList.isEmpty()) {
|
||||
Log.d(TAG, "没有自启动的Frpc")
|
||||
return@async
|
||||
}
|
||||
|
||||
for (frpc in frpcList) {
|
||||
if (!Frpclib.isRunning(frpc.uid)) {
|
||||
val error = Frpclib.runContent(frpc.uid, frpc.config)
|
||||
if (!TextUtils.isEmpty(error)) {
|
||||
Log.e(TAG, error)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//电量发生变化
|
||||
val levelCur: Int = intent.getIntExtra("level", 0)
|
||||
val levelPre: Int = SettingUtils.batteryLevelCurrent
|
||||
if (levelCur != levelPre) {
|
||||
var msg: String = BatteryUtils.getBatteryInfo(intent).toString()
|
||||
SettingUtils.batteryLevelCurrent = levelCur
|
||||
val levelMin: Int = SettingUtils.batteryLevelMin
|
||||
val levelMax: Int = SettingUtils.batteryLevelMax
|
||||
if (SettingUtils.batteryLevelOnce && levelMin > 0 && levelPre > levelCur && levelCur <= levelMin) { //电量下降到下限
|
||||
msg = String.format(getString(R.string.below_level_min), msg)
|
||||
sendMessage(context, msg)
|
||||
return
|
||||
} else if (SettingUtils.batteryLevelOnce && levelMax > 0 && levelPre < levelCur && levelCur >= levelMax) { //电量上升到上限
|
||||
msg = String.format(getString(R.string.over_level_max), msg)
|
||||
sendMessage(context, msg)
|
||||
return
|
||||
} else if (!SettingUtils.batteryLevelOnce && levelMin > 0 && levelPre > levelCur && levelCur == levelMin) { //电量下降到下限
|
||||
msg = String.format(getString(R.string.reach_level_min), msg)
|
||||
sendMessage(context, msg)
|
||||
return
|
||||
} else if (!SettingUtils.batteryLevelOnce && levelMax > 0 && levelPre < levelCur && levelCur == levelMax) { //电量上升到上限
|
||||
msg = String.format(getString(R.string.reach_level_max), msg)
|
||||
sendMessage(context, msg)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
//充电状态改变
|
||||
val status: Int = intent.getIntExtra("status", 0)
|
||||
if (SettingUtils.enableBatteryReceiver) {
|
||||
val oldStatus: Int = SettingUtils.batteryStatus
|
||||
if (status != oldStatus) {
|
||||
var msg: String = BatteryUtils.getBatteryInfo(intent).toString()
|
||||
SettingUtils.batteryStatus = status
|
||||
msg = getString(R.string.battery_status_changed) + BatteryUtils.getStatus(
|
||||
oldStatus
|
||||
) + " → " + BatteryUtils.getStatus(status) + msg
|
||||
sendMessage(context, msg)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//发送信息
|
||||
private fun sendMessage(context: Context, msg: String) {
|
||||
Log.i(TAG, msg)
|
||||
try {
|
||||
val msgInfo = MsgInfo("app", "88888888", msg, Date(), getString(R.string.battery_status_monitor), -1)
|
||||
val request = OneTimeWorkRequestBuilder<SendWorker>().setInputData(
|
||||
workDataOf(
|
||||
Worker.sendMsgInfo to Gson().toJson(msgInfo),
|
||||
)
|
||||
).build()
|
||||
WorkManager.getInstance(context).enqueue(request)
|
||||
} catch (e: Exception) {
|
||||
Log.e(TAG, "getLog e:" + e.message)
|
||||
}
|
||||
}
|
||||
|
||||
companion object {
|
||||
private const val TAG = "BatteryReceiver"
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue