Skip to content

Commit 4098974

Browse files
committed
feat: execute background sync work via WorkManager
1 parent d84f5b3 commit 4098974

File tree

17 files changed

+287
-280
lines changed

17 files changed

+287
-280
lines changed

app/src/main/AndroidManifest.xml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@ SPDX-License-Identifier: GPL-3.0-or-later
1212
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
1313
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
1414
<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE" />
15-
<uses-permission android:name="android.permission.SCHEDULE_EXACT_ALARM" />
16-
<uses-permission android:name ="android.permission.WAKE_LOCK" />
1715
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
1816

1917
<application

app/src/main/java/com/osfans/trime/TrimeApplication.kt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,10 @@ import com.osfans.trime.data.db.DraftHelper
1818
import com.osfans.trime.data.prefs.AppPrefs
1919
import com.osfans.trime.receiver.RimeIntentReceiver
2020
import com.osfans.trime.ui.main.LogActivity
21+
import com.osfans.trime.worker.BackgroundSyncWork
2122
import kotlinx.coroutines.CoroutineName
2223
import kotlinx.coroutines.MainScope
24+
import kotlinx.coroutines.launch
2325
import kotlinx.coroutines.plus
2426
import timber.log.Timber
2527
import kotlin.system.exitProcess
@@ -139,12 +141,19 @@ class TrimeApplication : Application() {
139141
CollectionHelper.init(applicationContext)
140142
DraftHelper.init(applicationContext)
141143
registerBroadcastReceiver()
144+
startWorkManager()
142145
} catch (e: Exception) {
143146
e.fillInStackTrace()
144147
return
145148
}
146149
}
147150

151+
private fun startWorkManager() {
152+
coroutineScope.launch {
153+
BackgroundSyncWork.start(applicationContext)
154+
}
155+
}
156+
148157
companion object {
149158
private var instance: TrimeApplication? = null
150159
private var lastPid: Int? = null

app/src/main/java/com/osfans/trime/daemon/RimeDaemon.kt

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,12 @@ object RimeDaemon {
9999
if (realRime.lifecycle.currentStateFlow.value == RimeLifecycle.State.STOPPED) {
100100
realRime.startup(false)
101101
}
102+
messageJob =
103+
TrimeApplication.getInstance().coroutineScope.launch {
104+
realRime.messageFlow.collect {
105+
handleRimeMessage(it)
106+
}
107+
}
102108
val session = establish(name)
103109
sessions[name] = session
104110
return@withLock session
@@ -112,6 +118,8 @@ object RimeDaemon {
112118
sessions -= name
113119
if (sessions.isEmpty()) {
114120
realRime.finalize()
121+
messageJob?.cancel()
122+
messageJob = null
115123
}
116124
}
117125

@@ -157,12 +165,6 @@ object RimeDaemon {
157165
.build()
158166
.let { notificationManager.notify(id, it) }
159167
realRime.finalize()
160-
messageJob =
161-
TrimeApplication.getInstance().coroutineScope.launch {
162-
realRime.messageFlow.collect {
163-
handleRimeMessage(it)
164-
}
165-
}
166168
realRime.startup(fullCheck)
167169
TrimeApplication.getInstance().coroutineScope.launch {
168170
// cancel notification on ready

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

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ class AppPrefs(
4141
val general = General(shared).register()
4242
val keyboard = Keyboard(shared)
4343
val theme = Theme(shared)
44-
val profile = Profile(shared)
44+
val profile = Profile(shared).register()
4545
val clipboard = Clipboard(shared)
4646
val other = Other(shared)
4747

@@ -249,17 +249,17 @@ class AppPrefs(
249249
) : PreferenceDelegateOwner(shared) {
250250
companion object {
251251
const val USER_DATA_DIR = "profile_user_data_dir"
252-
const val TIMING_BACKGROUND_SYNC_ENABLED = "profile_timing_background_sync"
253-
const val TIMING_BACKGROUND_SYNC_SET_TIME = "profile_timing_background_sync_set_time"
254-
const val LAST_BACKGROUND_SYNC_STATUS = "profile_last_background_sync_status"
255-
const val LAST_BACKGROUND_SYNC_TIME = "profile_last_background_sync_time"
252+
const val PERIODIC_BACKGROUND_SYNC = "periodic_background_sync"
253+
const val PERIODIC_BACKGROUND_SYNC_INTERVAL = "periodic_background_sync_interval"
254+
const val LAST_BACKGROUND_SYNC_STATUS = "last_background_sync_status"
255+
const val LAST_BACKGROUND_SYNC_TIME = "last_background_sync_time"
256256
}
257257

258258
var userDataDir by string(USER_DATA_DIR, DataManager.defaultDataDirectory.path)
259-
var timingBackgroundSyncEnabled by bool(TIMING_BACKGROUND_SYNC_ENABLED, false)
260-
var timingBackgroundSyncSetTime by long(TIMING_BACKGROUND_SYNC_SET_TIME, System.currentTimeMillis())
261-
var lastSyncStatus by bool(LAST_BACKGROUND_SYNC_STATUS, false)
262-
var lastBackgroundSyncTime by long(LAST_BACKGROUND_SYNC_TIME, 0L)
259+
val periodicBackgroundSync = bool(PERIODIC_BACKGROUND_SYNC, false)
260+
val periodicBackgroundSyncInterval = int(PERIODIC_BACKGROUND_SYNC_INTERVAL, 30)
261+
val lastBackgroundSyncStatus = bool(LAST_BACKGROUND_SYNC_STATUS, false)
262+
val lastBackgroundSyncTime = long(LAST_BACKGROUND_SYNC_TIME, 0L)
263263
}
264264

265265
class Clipboard(

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@ abstract class PreferenceDelegateOwner(
1515
protected fun int(
1616
key: String,
1717
defaultValue: Int,
18-
) = PreferenceDelegate(sharedPreferences, key, defaultValue)
18+
) = PreferenceDelegate(sharedPreferences, key, defaultValue).apply { register() }
1919

2020
protected fun long(
2121
key: String,
2222
defaultValue: Long,
23-
) = PreferenceDelegate(sharedPreferences, key, defaultValue)
23+
) = PreferenceDelegate(sharedPreferences, key, defaultValue).apply { register() }
2424

2525
protected fun float(
2626
key: String,
@@ -30,7 +30,7 @@ abstract class PreferenceDelegateOwner(
3030
protected fun bool(
3131
key: String,
3232
defaultValue: Boolean,
33-
): PreferenceDelegate<Boolean> = PreferenceDelegate(sharedPreferences, key, defaultValue)
33+
): PreferenceDelegate<Boolean> = PreferenceDelegate(sharedPreferences, key, defaultValue).apply { register() }
3434

3535
protected fun string(
3636
key: String,

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

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,7 @@ package com.osfans.trime.ime.core
66

77
import android.annotation.SuppressLint
88
import android.annotation.TargetApi
9-
import android.app.AlarmManager
109
import android.app.Dialog
11-
import android.app.PendingIntent
12-
import android.content.Intent
1310
import android.content.IntentFilter
1411
import android.content.res.Configuration
1512
import android.graphics.RectF
@@ -161,33 +158,6 @@ open class TrimeInputMethodService : LifecycleInputMethodService() {
161158
}
162159
}
163160

164-
/** 防止重启系统 强行停止应用时alarm任务丢失 */
165-
@SuppressLint("ScheduleExactAlarm")
166-
fun restartSystemStartTimingSync() {
167-
if (prefs.profile.timingBackgroundSyncEnabled) {
168-
val triggerTime = prefs.profile.timingBackgroundSyncSetTime
169-
val alarmManager = getSystemService(ALARM_SERVICE) as AlarmManager
170-
171-
/** 设置待发送的同步事件 */
172-
val pendingIntent =
173-
PendingIntent.getBroadcast(
174-
this,
175-
0,
176-
Intent("com.osfans.trime.timing.sync"),
177-
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
178-
PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE
179-
} else {
180-
PendingIntent.FLAG_UPDATE_CURRENT
181-
},
182-
)
183-
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { // 根据SDK设置alarm任务
184-
alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, triggerTime, pendingIntent)
185-
} else {
186-
alarmManager.setExact(AlarmManager.RTC_WAKEUP, triggerTime, pendingIntent)
187-
}
188-
}
189-
}
190-
191161
private fun registerReceiver() {
192162
val intentFilter =
193163
IntentFilter().apply {
@@ -231,7 +201,6 @@ open class TrimeInputMethodService : LifecycleInputMethodService() {
231201
ColorManager.init(resources.configuration)
232202
ThemeManager.init()
233203
InputFeedbackManager.init()
234-
restartSystemStartTimingSync()
235204
val theme = ThemeManager.activeTheme
236205
val defaultLocale = theme.generalStyle.locale.split(DELIMITER_SPLITTER)
237206
locales[0] =

app/src/main/java/com/osfans/trime/ui/components/TimePickerPreference.kt

Lines changed: 0 additions & 97 deletions
This file was deleted.

0 commit comments

Comments
 (0)