Skip to content

Commit 7e86995

Browse files
committed
refactors: optimize timing background sync
1 parent 3a2f450 commit 7e86995

File tree

10 files changed

+186
-187
lines changed

10 files changed

+186
-187
lines changed

app/src/main/java/com/osfans/trime/data/prefs/AppPrefs.kt

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import com.osfans.trime.ime.enums.FullscreenMode
1313
import com.osfans.trime.ime.enums.InlinePreeditMode
1414
import com.osfans.trime.util.appContext
1515
import java.lang.ref.WeakReference
16-
import java.util.Calendar
1716

1817
/**
1918
* Helper class for an organized access to the shared preferences.
@@ -198,19 +197,17 @@ class AppPrefs(
198197
) : PreferenceDelegateOwner(shared) {
199198
companion object {
200199
const val USER_DATA_DIR = "profile_user_data_dir"
201-
const val SYNC_BACKGROUND_ENABLED = "profile_sync_in_background"
202-
const val TIMING_SYNC_ENABLED = "profile_timing_sync"
203-
const val TIMING_SYNC_TRIGGER_TIME = "profile_timing_sync_trigger_time"
204-
const val LAST_SYNC_STATUS = "profile_last_sync_status"
205-
const val LAST_BACKGROUND_SYNC = "profile_last_background_sync"
200+
const val TIMING_BACKGROUND_SYNC_ENABLED = "profile_timing_background_sync"
201+
const val TIMING_BACKGROUND_SYNC_SET_TIME = "profile_timing_background_sync_set_time"
202+
const val LAST_BACKGROUND_SYNC_STATUS = "profile_last_background_sync_status"
203+
const val LAST_BACKGROUND_SYNC_TIME = "profile_last_background_sync_time"
206204
}
207205

208206
var userDataDir by string(USER_DATA_DIR, DataManager.defaultDataDirectory.path)
209-
var syncBackgroundEnabled by bool(SYNC_BACKGROUND_ENABLED, false)
210-
var timingSyncEnabled by bool(TIMING_SYNC_ENABLED, false)
211-
var timingSyncTriggerTime by long(TIMING_SYNC_TRIGGER_TIME, Calendar.getInstance().timeInMillis + 1200000L)
212-
var lastSyncStatus by bool(LAST_SYNC_STATUS, false)
213-
var lastBackgroundSync by string(LAST_BACKGROUND_SYNC, "")
207+
var timingBackgroundSyncEnabled by bool(TIMING_BACKGROUND_SYNC_ENABLED, false)
208+
var timingBackgroundSyncSetTime by long(TIMING_BACKGROUND_SYNC_SET_TIME, System.currentTimeMillis())
209+
var lastSyncStatus by bool(LAST_BACKGROUND_SYNC_STATUS, false)
210+
var lastBackgroundSyncTime by long(LAST_BACKGROUND_SYNC_TIME, 0L)
214211
}
215212

216213
class Clipboard(

app/src/main/java/com/osfans/trime/ime/broadcast/IntentReceiver.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ class IntentReceiver :
6565
wakeLock.acquire(600000) // 10分钟超时
6666
val cal = Calendar.getInstance()
6767
val triggerTime = cal.timeInMillis + TimeUnit.DAYS.toMillis(1) // 下次同步时间
68-
AppPrefs.defaultInstance().profile.timingSyncTriggerTime =
68+
AppPrefs.defaultInstance().profile.timingBackgroundSyncSetTime =
6969
triggerTime // 更新定时同步偏好值
7070
// 设置待发送的同步事件
7171
val pendingIntent =

app/src/main/java/com/osfans/trime/ime/core/TrimeInputMethodService.kt

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,6 @@ import android.content.Intent
1111
import android.content.res.Configuration
1212
import android.inputmethodservice.InputMethodService
1313
import android.os.Build
14-
import android.os.Handler
15-
import android.os.Looper
16-
import android.os.Message
1714
import android.os.SystemClock
1815
import android.text.InputType
1916
import android.view.InputDevice
@@ -160,11 +157,6 @@ open class TrimeInputMethodService : LifecycleInputMethodService() {
160157
Timber.d("onWindowHidden")
161158
}
162159
isWindowShown = false
163-
if (prefs.profile.syncBackgroundEnabled) {
164-
val msg = Message()
165-
msg.obj = this
166-
syncBackgroundHandler.sendMessageDelayed(msg, 5000) // 输入面板隐藏5秒后,开始后台同步
167-
}
168160
}
169161

170162
private fun updateRimeOption(): Boolean {
@@ -184,8 +176,8 @@ open class TrimeInputMethodService : LifecycleInputMethodService() {
184176
/** 防止重启系统 强行停止应用时alarm任务丢失 */
185177
@SuppressLint("ScheduleExactAlarm")
186178
fun restartSystemStartTimingSync() {
187-
if (prefs.profile.timingSyncEnabled) {
188-
val triggerTime = prefs.profile.timingSyncTriggerTime
179+
if (prefs.profile.timingBackgroundSyncEnabled) {
180+
val triggerTime = prefs.profile.timingBackgroundSyncSetTime
189181
val alarmManager = getSystemService(ALARM_SERVICE) as AlarmManager
190182

191183
/** 设置待发送的同步事件 */
@@ -1074,18 +1066,5 @@ open class TrimeInputMethodService : LifecycleInputMethodService() {
10741066
fun getService(): TrimeInputMethodService = instance ?: throw IllegalStateException("TrimeInputMethodService is not initialized")
10751067

10761068
fun getServiceOrNull(): TrimeInputMethodService? = instance
1077-
1078-
private val syncBackgroundHandler =
1079-
Handler(
1080-
Looper.getMainLooper(),
1081-
) { msg: Message ->
1082-
// 若当前没有输入面板,则后台同步。防止面板关闭后5秒内再次打开
1083-
val service = msg.obj as TrimeInputMethodService
1084-
if (!service.isShowInputRequested) {
1085-
ShortcutUtils.syncInBackground()
1086-
service.shouldUpdateRimeOption = true
1087-
}
1088-
false
1089-
}
10901069
}
10911070
}
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
/*
2+
* SPDX-FileCopyrightText: 2015 - 2024 Rime community
3+
* SPDX-License-Identifier: GPL-3.0-or-later
4+
*/
5+
6+
package com.osfans.trime.ui.components
7+
8+
import android.app.TimePickerDialog
9+
import android.content.Context
10+
import android.util.AttributeSet
11+
import androidx.core.content.edit
12+
import androidx.preference.DialogPreference
13+
import java.util.Calendar
14+
import java.util.concurrent.TimeUnit
15+
16+
class TimePickerPreference
17+
@JvmOverloads
18+
constructor(
19+
context: Context,
20+
attrs: AttributeSet? = null,
21+
) : DialogPreference(context, attrs) {
22+
var default: Long = System.currentTimeMillis()
23+
24+
var value = System.currentTimeMillis()
25+
private set
26+
27+
override fun onSetInitialValue(defaultValue: Any?) {
28+
super.onSetInitialValue(defaultValue)
29+
preferenceDataStore?.apply {
30+
value = getLong(key, default)
31+
} ?: sharedPreferences?.apply {
32+
value = getLong(key, default)
33+
}
34+
}
35+
36+
override fun setDefaultValue(defaultValue: Any?) {
37+
val time = defaultValue as? Long ?: return
38+
value = time as? Long ?: System.currentTimeMillis()
39+
}
40+
41+
private fun persistValues(setTime: Long) {
42+
if (!shouldPersist()) return
43+
value = setTime
44+
preferenceDataStore?.apply {
45+
putLong(key, setTime)
46+
} ?: sharedPreferences?.edit {
47+
putLong(key, setTime)
48+
}
49+
}
50+
51+
override fun onClick() {
52+
showTimePickerDialog()
53+
}
54+
55+
private fun showTimePickerDialog() {
56+
val cal = Calendar.getInstance()
57+
val timeSetListener = // 监听时间选择器设置
58+
TimePickerDialog.OnTimeSetListener { _, hour, minute ->
59+
cal.set(Calendar.HOUR_OF_DAY, hour)
60+
cal.set(Calendar.MINUTE, minute)
61+
val timeInMillis = cal.timeInMillis // 设置的时间
62+
val triggerAtMillis =
63+
if (timeInMillis > System.currentTimeMillis() + PERIOD) {
64+
timeInMillis
65+
} else {
66+
// 设置的时间小于当前时间 20 分钟时将同步推迟到明天
67+
timeInMillis + TimeUnit.DAYS.toMillis(1)
68+
}
69+
setValue(triggerAtMillis)
70+
}
71+
// 时间选择器设置
72+
TimePickerDialog(
73+
context,
74+
timeSetListener,
75+
cal.get(Calendar.HOUR_OF_DAY),
76+
cal.get(Calendar.MINUTE),
77+
true,
78+
).apply {
79+
setOnCancelListener {
80+
// 当取消时间选择器时重置偏好
81+
setValue(0L)
82+
}
83+
show()
84+
}
85+
}
86+
87+
private fun setValue(setTime: Long) {
88+
if (callChangeListener(setTime)) {
89+
persistValues(setTime)
90+
notifyChanged()
91+
}
92+
}
93+
94+
companion object {
95+
private const val PERIOD = 1_200_000L // 20 分钟
96+
}
97+
}

0 commit comments

Comments
 (0)