Skip to content

Commit

Permalink
refactors: optimize timing background sync
Browse files Browse the repository at this point in the history
  • Loading branch information
WhiredPlanck committed Oct 7, 2024
1 parent 6817f1e commit 9ef7692
Show file tree
Hide file tree
Showing 10 changed files with 186 additions and 187 deletions.
19 changes: 8 additions & 11 deletions app/src/main/java/com/osfans/trime/data/prefs/AppPrefs.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import com.osfans.trime.ime.enums.FullscreenMode
import com.osfans.trime.ime.enums.InlinePreeditMode
import com.osfans.trime.util.appContext
import java.lang.ref.WeakReference
import java.util.Calendar

/**
* Helper class for an organized access to the shared preferences.
Expand Down Expand Up @@ -198,19 +197,17 @@ class AppPrefs(
) : PreferenceDelegateOwner(shared) {
companion object {
const val USER_DATA_DIR = "profile_user_data_dir"
const val SYNC_BACKGROUND_ENABLED = "profile_sync_in_background"
const val TIMING_SYNC_ENABLED = "profile_timing_sync"
const val TIMING_SYNC_TRIGGER_TIME = "profile_timing_sync_trigger_time"
const val LAST_SYNC_STATUS = "profile_last_sync_status"
const val LAST_BACKGROUND_SYNC = "profile_last_background_sync"
const val TIMING_BACKGROUND_SYNC_ENABLED = "profile_timing_background_sync"
const val TIMING_BACKGROUND_SYNC_SET_TIME = "profile_timing_sync_set_time"
const val LAST_BACKGROUND_SYNC_STATUS = "profile_last_background_sync_status"
const val LAST_BACKGROUND_SYNC_TIME = "profile_last_background_sync_time"
}

var userDataDir by string(USER_DATA_DIR, DataManager.defaultDataDirectory.path)
var syncBackgroundEnabled by bool(SYNC_BACKGROUND_ENABLED, false)
var timingSyncEnabled by bool(TIMING_SYNC_ENABLED, false)
var timingSyncTriggerTime by long(TIMING_SYNC_TRIGGER_TIME, Calendar.getInstance().timeInMillis + 1200000L)
var lastSyncStatus by bool(LAST_SYNC_STATUS, false)
var lastBackgroundSync by string(LAST_BACKGROUND_SYNC, "")
var timingBackgroundSyncEnabled by bool(TIMING_BACKGROUND_SYNC_ENABLED, false)
var timingBackgroundSyncSetTime by long(TIMING_BACKGROUND_SYNC_SET_TIME, System.currentTimeMillis())
var lastSyncStatus by bool(LAST_BACKGROUND_SYNC_STATUS, false)
var lastBackgroundSyncTime by long(LAST_BACKGROUND_SYNC_TIME, 0L)
}

class Clipboard(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ class IntentReceiver :
wakeLock.acquire(600000) // 10分钟超时
val cal = Calendar.getInstance()
val triggerTime = cal.timeInMillis + TimeUnit.DAYS.toMillis(1) // 下次同步时间
AppPrefs.defaultInstance().profile.timingSyncTriggerTime =
AppPrefs.defaultInstance().profile.timingBackgroundSyncSetTime =
triggerTime // 更新定时同步偏好值
// 设置待发送的同步事件
val pendingIntent =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@ import android.content.Intent
import android.content.res.Configuration
import android.inputmethodservice.InputMethodService
import android.os.Build
import android.os.Handler
import android.os.Looper
import android.os.Message
import android.os.SystemClock
import android.text.InputType
import android.view.InputDevice
Expand Down Expand Up @@ -160,11 +157,6 @@ open class TrimeInputMethodService : LifecycleInputMethodService() {
Timber.d("onWindowHidden")
}
isWindowShown = false
if (prefs.profile.syncBackgroundEnabled) {
val msg = Message()
msg.obj = this
syncBackgroundHandler.sendMessageDelayed(msg, 5000) // 输入面板隐藏5秒后,开始后台同步
}
}

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

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

fun getServiceOrNull(): TrimeInputMethodService? = instance

private val syncBackgroundHandler =
Handler(
Looper.getMainLooper(),
) { msg: Message ->
// 若当前没有输入面板,则后台同步。防止面板关闭后5秒内再次打开
val service = msg.obj as TrimeInputMethodService
if (!service.isShowInputRequested) {
ShortcutUtils.syncInBackground()
service.shouldUpdateRimeOption = true
}
false
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
/*
* SPDX-FileCopyrightText: 2015 - 2024 Rime community
* SPDX-License-Identifier: GPL-3.0-or-later
*/

package com.osfans.trime.ui.components

import android.app.TimePickerDialog
import android.content.Context
import android.util.AttributeSet
import androidx.core.content.edit
import androidx.preference.DialogPreference
import java.util.Calendar
import java.util.concurrent.TimeUnit

class TimePickerPreference
@JvmOverloads
constructor(
context: Context,
attrs: AttributeSet? = null,
) : DialogPreference(context, attrs) {
var default: Long = System.currentTimeMillis()

var value = System.currentTimeMillis()
private set

override fun onSetInitialValue(defaultValue: Any?) {
super.onSetInitialValue(defaultValue)
preferenceDataStore?.apply {
value = getLong(key, default)
} ?: sharedPreferences?.apply {
value = getLong(key, default)
}
}

override fun setDefaultValue(defaultValue: Any?) {
val time = defaultValue as? Long ?: return
value = time as? Long ?: System.currentTimeMillis()
}

private fun persistValues(setTime: Long) {
if (!shouldPersist()) return
value = setTime
preferenceDataStore?.apply {
putLong(key, setTime)
} ?: sharedPreferences?.edit {
putLong(key, setTime)
}
}

override fun onClick() {
showTimePickerDialog()
}

private fun showTimePickerDialog() {
val cal = Calendar.getInstance()
val timeSetListener = // 监听时间选择器设置
TimePickerDialog.OnTimeSetListener { _, hour, minute ->
cal.set(Calendar.HOUR_OF_DAY, hour)
cal.set(Calendar.MINUTE, minute)
val timeInMillis = cal.timeInMillis // 设置的时间
val triggerAtMillis =
if (timeInMillis > System.currentTimeMillis() + PERIOD) {
timeInMillis
} else {
// 设置的时间小于当前时间 20 分钟时将同步推迟到明天
timeInMillis + TimeUnit.DAYS.toMillis(1)
}
setValue(triggerAtMillis)
}
// 时间选择器设置
TimePickerDialog(
context,
timeSetListener,
cal.get(Calendar.HOUR_OF_DAY),
cal.get(Calendar.MINUTE),
true,
).apply {
setOnCancelListener {
// 当取消时间选择器时重置偏好
setValue(0L)
}
show()
}
}

private fun setValue(setTime: Long) {
if (callChangeListener(setTime)) {
persistValues(setTime)
notifyChanged()
}
}

companion object {
private const val PERIOD = 1_200_000L // 20 分钟
}
}
Loading

0 comments on commit 9ef7692

Please sign in to comment.