Skip to content

Commit

Permalink
新增:自动删除N天前的转发记录(0=禁用,触发机制:每次电量变化时扫描) #224
Browse files Browse the repository at this point in the history
  • Loading branch information
pppscn committed Sep 22, 2022
1 parent c2df047 commit 66e324b
Show file tree
Hide file tree
Showing 10 changed files with 378 additions and 64 deletions.
15 changes: 12 additions & 3 deletions app/src/main/java/com/idormy/sms/forwarder/database/AppDatabase.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import androidx.room.RoomDatabase
import androidx.room.TypeConverters
import androidx.room.migration.Migration
import androidx.sqlite.db.SupportSQLiteDatabase
import com.idormy.sms.forwarder.BuildConfig
import com.idormy.sms.forwarder.database.dao.FrpcDao
import com.idormy.sms.forwarder.database.dao.LogsDao
import com.idormy.sms.forwarder.database.dao.RuleDao
Expand Down Expand Up @@ -43,7 +44,11 @@ abstract class AppDatabase : RoomDatabase() {
}

private fun buildDatabase(context: Context): AppDatabase {
return Room.databaseBuilder(context.applicationContext, AppDatabase::class.java, DATABASE_NAME)
var builder = Room.databaseBuilder(
context.applicationContext,
AppDatabase::class.java,
DATABASE_NAME
)
.allowMainThreadQueries() //TODO:允许主线程访问,后面再优化
.addCallback(object : RoomDatabase.Callback() {
override fun onCreate(db: SupportSQLiteDatabase) {
Expand Down Expand Up @@ -93,10 +98,14 @@ custom_domains = smsf.demo.com
MIGRATION_8_9,
MIGRATION_9_10,
)
.setQueryCallback({ sqlQuery, bindArgs ->

if (BuildConfig.DEBUG) {
builder.setQueryCallback({ sqlQuery, bindArgs ->
println("SQL_QUERY: $sqlQuery\nBIND_ARGS: $bindArgs")
}, Executors.newSingleThreadExecutor())
.build()
}

return builder.build()
}

//转发日志添加SIM卡槽信息
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ interface LogsDao {
@Query("DELETE FROM Logs where type=:type")
fun deleteAll(type: String): Completable

@Query("DELETE FROM Logs where time<:time")
fun deleteTimeAgo(time: Long)

@Update
fun update(logs: Logs): Completable

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,15 @@ class LogsRepository(private val logsDao: LogsDao) {
logsDao.delete(id)
}

@WorkerThread
fun deleteTimeAgo(time: Long) {
logsDao.deleteTimeAgo(time)
}

@WorkerThread
suspend fun insert(logs: Logs): Long = logsDao.insert(logs)

@WorkerThread
fun updateStatus(id: Long, status: Int, response: String): Int = logsDao.updateStatus(id, status, response)
fun updateStatus(id: Long, status: Int, response: String): Int =
logsDao.updateStatus(id, status, response)
}
161 changes: 126 additions & 35 deletions app/src/main/java/com/idormy/sms/forwarder/fragment/SettingsFragment.kt

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import androidx.work.WorkManager
import androidx.work.workDataOf
import com.google.gson.Gson
import com.idormy.sms.forwarder.R
import com.idormy.sms.forwarder.core.Core
import com.idormy.sms.forwarder.entity.MsgInfo
import com.idormy.sms.forwarder.utils.BatteryUtils
import com.idormy.sms.forwarder.utils.SettingUtils
Expand Down Expand Up @@ -58,6 +59,13 @@ class BatteryService : Service() {
private val batteryReceiver: BroadcastReceiver = object : BroadcastReceiver() {
@SuppressLint("DefaultLocale")
override fun onReceive(context: Context, intent: Intent) {
//自动删除N天前的转发记录
if (SettingUtils.autoCleanLogsDays > 0) {
Log.d(TAG, "自动删除N天前的转发记录")
val cal = Calendar.getInstance()
cal.add(Calendar.DAY_OF_MONTH, 0 - SettingUtils.autoCleanLogsDays)
Core.logs.deleteTimeAgo(cal.timeInMillis)
}

//电量发生变化
val levelCur: Int = intent.getIntExtra("level", 0)
Expand Down Expand Up @@ -93,7 +101,9 @@ class BatteryService : Service() {
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
msg = getString(R.string.battery_status_changed) + BatteryUtils.getStatus(
oldStatus
) + "" + BatteryUtils.getStatus(status) + msg
sendMessage(context, msg)
}
}
Expand All @@ -104,7 +114,14 @@ class BatteryService : Service() {
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 msgInfo = MsgInfo(
"app",
"88888888",
msg,
Date(),
getString(R.string.battery_status_monitor),
-1
)
val request = OneTimeWorkRequestBuilder<SendWorker>()
.setInputData(
workDataOf(
Expand Down
169 changes: 148 additions & 21 deletions app/src/main/java/com/idormy/sms/forwarder/utils/Constants.kt
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ const val ENABLE_LOAD_SYSTEM_APP_LIST = "enable_load_system_app_list"
const val SP_DUPLICATE_MESSAGES_LIMITS = "duplicate_messages_limits"
const val SP_SILENT_PERIOD_START = "silent_period_start"
const val SP_SILENT_PERIOD_END = "silent_period_end"
const val SP_AUTO_CLEAN_LOGS_DAYS = "auto_clean_logs_days"

const val SP_BATTERY_RECEIVER = "enable_battery_receiver"
const val SP_BATTERY_STATUS = "battery_status"
Expand Down Expand Up @@ -164,20 +165,104 @@ const val TYPE_GOTIFY = 11
const val TYPE_DINGTALK_INNER_ROBOT = 12
const val TYPE_FEISHU_APP = 13
var SENDER_FRAGMENT_LIST = listOf(
PageInfo(getString(R.string.dingtalk_robot), "com.idormy.sms.forwarder.fragment.senders.DingtalkGroupRobotFragment", "{\"\":\"\"}", CoreAnim.slide, R.drawable.icon_dingtalk),
PageInfo(getString(R.string.email), "com.idormy.sms.forwarder.fragment.senders.EmailFragment", "{\"\":\"\"}", CoreAnim.slide, R.drawable.icon_email),
PageInfo(getString(R.string.bark), "com.idormy.sms.forwarder.fragment.senders.BarkFragment", "{\"\":\"\"}", CoreAnim.slide, R.drawable.icon_bark),
PageInfo(getString(R.string.webhook), "com.idormy.sms.forwarder.fragment.senders.WebhookFragment", "{\"\":\"\"}", CoreAnim.slide, R.drawable.icon_webhook),
PageInfo(getString(R.string.wework_robot), "com.idormy.sms.forwarder.fragment.senders.WeworkRobotFragment", "{\"\":\"\"}", CoreAnim.slide, R.drawable.icon_wework_robot),
PageInfo(getString(R.string.wework_agent), "com.idormy.sms.forwarder.fragment.senders.WeworkAgentFragment", "{\"\":\"\"}", CoreAnim.slide, R.drawable.icon_wework_agent),
PageInfo(getString(R.string.server_chan), "com.idormy.sms.forwarder.fragment.senders.ServerchanFragment", "{\"\":\"\"}", CoreAnim.slide, R.drawable.icon_serverchan),
PageInfo(getString(R.string.telegram), "com.idormy.sms.forwarder.fragment.senders.TelegramFragment", "{\"\":\"\"}", CoreAnim.slide, R.drawable.icon_telegram),
PageInfo(getString(R.string.sms_menu), "com.idormy.sms.forwarder.fragment.senders.SmsFragment", "{\"\":\"\"}", CoreAnim.slide, R.drawable.icon_sms),
PageInfo(getString(R.string.feishu), "com.idormy.sms.forwarder.fragment.senders.FeishuFragment", "{\"\":\"\"}", CoreAnim.slide, R.drawable.icon_feishu),
PageInfo(getString(R.string.pushplus), "com.idormy.sms.forwarder.fragment.senders.PushplusFragment", "{\"\":\"\"}", CoreAnim.slide, R.drawable.icon_pushplus),
PageInfo(getString(R.string.gotify), "com.idormy.sms.forwarder.fragment.senders.GotifyFragment", "{\"\":\"\"}", CoreAnim.slide, R.drawable.icon_gotify),
PageInfo(getString(R.string.dingtalk_inner_robot), "com.idormy.sms.forwarder.fragment.senders.DingtalkInnerRobotFragment", "{\"\":\"\"}", CoreAnim.slide, R.drawable.icon_dingtalk_inner),
PageInfo(getString(R.string.feishu_app), "com.idormy.sms.forwarder.fragment.senders.FeishuAppFragment", "{\"\":\"\"}", CoreAnim.slide, R.drawable.icon_feishu_app),
PageInfo(
getString(R.string.dingtalk_robot),
"com.idormy.sms.forwarder.fragment.senders.DingtalkGroupRobotFragment",
"{\"\":\"\"}",
CoreAnim.slide,
R.drawable.icon_dingtalk
),
PageInfo(
getString(R.string.email),
"com.idormy.sms.forwarder.fragment.senders.EmailFragment",
"{\"\":\"\"}",
CoreAnim.slide,
R.drawable.icon_email
),
PageInfo(
getString(R.string.bark),
"com.idormy.sms.forwarder.fragment.senders.BarkFragment",
"{\"\":\"\"}",
CoreAnim.slide,
R.drawable.icon_bark
),
PageInfo(
getString(R.string.webhook),
"com.idormy.sms.forwarder.fragment.senders.WebhookFragment",
"{\"\":\"\"}",
CoreAnim.slide,
R.drawable.icon_webhook
),
PageInfo(
getString(R.string.wework_robot),
"com.idormy.sms.forwarder.fragment.senders.WeworkRobotFragment",
"{\"\":\"\"}",
CoreAnim.slide,
R.drawable.icon_wework_robot
),
PageInfo(
getString(R.string.wework_agent),
"com.idormy.sms.forwarder.fragment.senders.WeworkAgentFragment",
"{\"\":\"\"}",
CoreAnim.slide,
R.drawable.icon_wework_agent
),
PageInfo(
getString(R.string.server_chan),
"com.idormy.sms.forwarder.fragment.senders.ServerchanFragment",
"{\"\":\"\"}",
CoreAnim.slide,
R.drawable.icon_serverchan
),
PageInfo(
getString(R.string.telegram),
"com.idormy.sms.forwarder.fragment.senders.TelegramFragment",
"{\"\":\"\"}",
CoreAnim.slide,
R.drawable.icon_telegram
),
PageInfo(
getString(R.string.sms_menu),
"com.idormy.sms.forwarder.fragment.senders.SmsFragment",
"{\"\":\"\"}",
CoreAnim.slide,
R.drawable.icon_sms
),
PageInfo(
getString(R.string.feishu),
"com.idormy.sms.forwarder.fragment.senders.FeishuFragment",
"{\"\":\"\"}",
CoreAnim.slide,
R.drawable.icon_feishu
),
PageInfo(
getString(R.string.pushplus),
"com.idormy.sms.forwarder.fragment.senders.PushplusFragment",
"{\"\":\"\"}",
CoreAnim.slide,
R.drawable.icon_pushplus
),
PageInfo(
getString(R.string.gotify),
"com.idormy.sms.forwarder.fragment.senders.GotifyFragment",
"{\"\":\"\"}",
CoreAnim.slide,
R.drawable.icon_gotify
),
PageInfo(
getString(R.string.dingtalk_inner_robot),
"com.idormy.sms.forwarder.fragment.senders.DingtalkInnerRobotFragment",
"{\"\":\"\"}",
CoreAnim.slide,
R.drawable.icon_dingtalk_inner
),
PageInfo(
getString(R.string.feishu_app),
"com.idormy.sms.forwarder.fragment.senders.FeishuAppFragment",
"{\"\":\"\"}",
CoreAnim.slide,
R.drawable.icon_feishu_app
),
)

//前台服务
Expand Down Expand Up @@ -252,11 +337,53 @@ const val SP_SERVER_HISTORY = "server_history"
const val SP_SERVER_CONFIG = "server_config"
const val SP_CLIENT_SIGN_KEY = "client_sign_key"
var CLIENT_FRAGMENT_LIST = listOf(
PageInfo(getString(R.string.api_clone), "com.idormy.sms.forwarder.fragment.client.CloneFragment", "{\"\":\"\"}", CoreAnim.slide, R.drawable.icon_api_clone),
PageInfo(getString(R.string.api_sms_send), "com.idormy.sms.forwarder.fragment.client.SmsSendFragment", "{\"\":\"\"}", CoreAnim.slide, R.drawable.icon_api_sms_send),
PageInfo(getString(R.string.api_sms_query), "com.idormy.sms.forwarder.fragment.client.SmsQueryFragment", "{\"\":\"\"}", CoreAnim.slide, R.drawable.icon_api_sms_query),
PageInfo(getString(R.string.api_call_query), "com.idormy.sms.forwarder.fragment.client.CallQueryFragment", "{\"\":\"\"}", CoreAnim.slide, R.drawable.icon_api_call_query),
PageInfo(getString(R.string.api_contact_query), "com.idormy.sms.forwarder.fragment.client.ContactQueryFragment", "{\"\":\"\"}", CoreAnim.slide, R.drawable.icon_api_contact_query),
PageInfo(getString(R.string.api_battery_query), "com.idormy.sms.forwarder.fragment.client.BatteryQueryFragment", "{\"\":\"\"}", CoreAnim.slide, R.drawable.icon_api_battery_query),
PageInfo(getString(R.string.api_wol), "com.idormy.sms.forwarder.fragment.client.WolSendFragment", "{\"\":\"\"}", CoreAnim.slide, R.drawable.icon_api_wol),
PageInfo(
getString(R.string.api_clone),
"com.idormy.sms.forwarder.fragment.client.CloneFragment",
"{\"\":\"\"}",
CoreAnim.slide,
R.drawable.icon_api_clone
),
PageInfo(
getString(R.string.api_sms_send),
"com.idormy.sms.forwarder.fragment.client.SmsSendFragment",
"{\"\":\"\"}",
CoreAnim.slide,
R.drawable.icon_api_sms_send
),
PageInfo(
getString(R.string.api_sms_query),
"com.idormy.sms.forwarder.fragment.client.SmsQueryFragment",
"{\"\":\"\"}",
CoreAnim.slide,
R.drawable.icon_api_sms_query
),
PageInfo(
getString(R.string.api_call_query),
"com.idormy.sms.forwarder.fragment.client.CallQueryFragment",
"{\"\":\"\"}",
CoreAnim.slide,
R.drawable.icon_api_call_query
),
PageInfo(
getString(R.string.api_contact_query),
"com.idormy.sms.forwarder.fragment.client.ContactQueryFragment",
"{\"\":\"\"}",
CoreAnim.slide,
R.drawable.icon_api_contact_query
),
PageInfo(
getString(R.string.api_battery_query),
"com.idormy.sms.forwarder.fragment.client.BatteryQueryFragment",
"{\"\":\"\"}",
CoreAnim.slide,
R.drawable.icon_api_battery_query
),
PageInfo(
getString(R.string.api_wol),
"com.idormy.sms.forwarder.fragment.client.WolSendFragment",
"{\"\":\"\"}",
CoreAnim.slide,
R.drawable.icon_api_wol
),
)
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,14 @@ class SettingUtils private constructor() {
MMKVUtils.put(SP_SILENT_PERIOD_END, silentPeriodEnd)
}

//自动删除N天前的转发记录
@JvmStatic
var autoCleanLogsDays: Int
get() = MMKVUtils.getInt(SP_AUTO_CLEAN_LOGS_DAYS, 0)
set(autoCleanLogsDays) {
MMKVUtils.put(SP_AUTO_CLEAN_LOGS_DAYS, autoCleanLogsDays)
}

//是否监听电池状态变化
@JvmStatic
var enableBatteryReceiver: Boolean
Expand Down
47 changes: 47 additions & 0 deletions app/src/main/res/layout/fragment_settings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,53 @@

</LinearLayout>

<LinearLayout
style="@style/settingBarStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingEnd="15dp"
tools:ignore="RtlSymmetry">

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/auto_clean_logs"
android:textStyle="bold"
tools:ignore="RelativeOverlap" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/auto_clean_logs_tips"
android:textSize="9sp"
tools:ignore="SmallSp" />

</LinearLayout>

<com.xuexiang.xui.widget.picker.XSeekBar
android:id="@+id/xsb_auto_clean_logs"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="5dp"
android:layout_weight="1"
app:xsb_max="60"
app:xsb_min="0" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="5dp"
android:text="@string/day"
android:textSize="12sp"
android:textStyle="bold" />

</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
Expand Down
5 changes: 4 additions & 1 deletion app/src/main/res/values-en/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -684,7 +684,7 @@
<string name="bark_level_active">Immediately display notifications</string>
<string name="bark_level_timeSensitive">Time-sensitive notifications that can be displayed in a focused state</string>
<string name="bark_level_passive">Only added to the notification list, no screen reminder</string>
<string name="bark_url_regex" tools:ignore="TypographyDashes" formatted="false"><![CDATA[^(https?|ftp|file)://[-a-zA-Z0-9+&@#/%?=~_|!:,.;]*[-a-zA-Z0-9+&@#/%=~_|]]]></string>
<string name="bark_url_regex" formatted="false" tools:ignore="TypographyDashes"><![CDATA[^(https?|ftp|file)://[-a-zA-Z0-9+&@#/%?=~_|!:,.;]*[-a-zA-Z0-9+&@#/%=~_|]]]></string>
<string name="bark_url_error">Url format error</string>

<string name="from_email_hint">Fill in the username before @</string>
Expand Down Expand Up @@ -916,4 +916,7 @@
<string name="app_id">App ID</string>
<string name="app_secret">App Secret</string>
<string name="user_id">User ID</string>
<string name="auto_clean_logs">Auto delete logs N days ago</string>
<string name="auto_clean_logs_tips">0=disabled, scan when battery change</string>
<string name="day">天</string>
</resources>
Loading

0 comments on commit 66e324b

Please sign in to comment.