Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Background sync with workmanager #1482

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ dependencies {
implementation(libs.androidx.room.runtime)
implementation(libs.androidx.room.ktx)
implementation(libs.androidx.viewpager2)
implementation(libs.androidx.work.runtime.ktx)
implementation(libs.flexbox)
implementation(libs.bravh)
implementation(libs.kaml)
Expand Down
2 changes: 0 additions & 2 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ SPDX-License-Identifier: GPL-3.0-or-later
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.SCHEDULE_EXACT_ALARM" />
<uses-permission android:name ="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />

<application
Expand Down
9 changes: 9 additions & 0 deletions app/src/main/java/com/osfans/trime/TrimeApplication.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ import com.osfans.trime.data.db.DraftHelper
import com.osfans.trime.data.prefs.AppPrefs
import com.osfans.trime.receiver.RimeIntentReceiver
import com.osfans.trime.ui.main.LogActivity
import com.osfans.trime.worker.BackgroundSyncWork
import kotlinx.coroutines.CoroutineName
import kotlinx.coroutines.MainScope
import kotlinx.coroutines.launch
import kotlinx.coroutines.plus
import timber.log.Timber
import kotlin.system.exitProcess
Expand Down Expand Up @@ -139,12 +141,19 @@ class TrimeApplication : Application() {
CollectionHelper.init(applicationContext)
DraftHelper.init(applicationContext)
registerBroadcastReceiver()
startWorkManager()
} catch (e: Exception) {
e.fillInStackTrace()
return
}
}

private fun startWorkManager() {
coroutineScope.launch {
BackgroundSyncWork.start(applicationContext)
}
}

companion object {
private var instance: TrimeApplication? = null
private var lastPid: Int? = null
Expand Down
14 changes: 8 additions & 6 deletions app/src/main/java/com/osfans/trime/daemon/RimeDaemon.kt
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,12 @@ object RimeDaemon {
if (realRime.lifecycle.currentStateFlow.value == RimeLifecycle.State.STOPPED) {
realRime.startup(false)
}
messageJob =
TrimeApplication.getInstance().coroutineScope.launch {
realRime.messageFlow.collect {
handleRimeMessage(it)
}
}
val session = establish(name)
sessions[name] = session
return@withLock session
Expand All @@ -112,6 +118,8 @@ object RimeDaemon {
sessions -= name
if (sessions.isEmpty()) {
realRime.finalize()
messageJob?.cancel()
messageJob = null
}
}

Expand Down Expand Up @@ -157,12 +165,6 @@ object RimeDaemon {
.build()
.let { notificationManager.notify(id, it) }
realRime.finalize()
messageJob =
TrimeApplication.getInstance().coroutineScope.launch {
realRime.messageFlow.collect {
handleRimeMessage(it)
}
}
realRime.startup(fullCheck)
TrimeApplication.getInstance().coroutineScope.launch {
// cancel notification on ready
Expand Down
18 changes: 9 additions & 9 deletions app/src/main/java/com/osfans/trime/data/prefs/AppPrefs.kt
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class AppPrefs(
val general = General(shared).register()
val keyboard = Keyboard(shared)
val theme = Theme(shared)
val profile = Profile(shared)
val profile = Profile(shared).register()
val clipboard = Clipboard(shared)
val other = Other(shared)

Expand Down Expand Up @@ -247,17 +247,17 @@ class AppPrefs(
) : PreferenceDelegateOwner(shared) {
companion object {
const val USER_DATA_DIR = "profile_user_data_dir"
const val TIMING_BACKGROUND_SYNC_ENABLED = "profile_timing_background_sync"
const val TIMING_BACKGROUND_SYNC_SET_TIME = "profile_timing_background_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"
const val PERIODIC_BACKGROUND_SYNC = "periodic_background_sync"
const val PERIODIC_BACKGROUND_SYNC_INTERVAL = "periodic_background_sync_interval"
const val LAST_BACKGROUND_SYNC_STATUS = "last_background_sync_status"
const val LAST_BACKGROUND_SYNC_TIME = "last_background_sync_time"
}

var userDataDir by string(USER_DATA_DIR, DataManager.defaultDataDirectory.path)
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)
val periodicBackgroundSync = bool(PERIODIC_BACKGROUND_SYNC, false)
val periodicBackgroundSyncInterval = int(PERIODIC_BACKGROUND_SYNC_INTERVAL, 30)
val lastBackgroundSyncStatus = bool(LAST_BACKGROUND_SYNC_STATUS, false)
val lastBackgroundSyncTime = long(LAST_BACKGROUND_SYNC_TIME, 0L)
}

class Clipboard(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ abstract class PreferenceDelegateOwner(
protected fun int(
key: String,
defaultValue: Int,
) = PreferenceDelegate(sharedPreferences, key, defaultValue)
) = PreferenceDelegate(sharedPreferences, key, defaultValue).apply { register() }

protected fun long(
key: String,
defaultValue: Long,
) = PreferenceDelegate(sharedPreferences, key, defaultValue)
) = PreferenceDelegate(sharedPreferences, key, defaultValue).apply { register() }

protected fun float(
key: String,
Expand All @@ -30,7 +30,7 @@ abstract class PreferenceDelegateOwner(
protected fun bool(
key: String,
defaultValue: Boolean,
): PreferenceDelegate<Boolean> = PreferenceDelegate(sharedPreferences, key, defaultValue)
): PreferenceDelegate<Boolean> = PreferenceDelegate(sharedPreferences, key, defaultValue).apply { register() }

protected fun string(
key: String,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@ package com.osfans.trime.ime.core

import android.annotation.SuppressLint
import android.annotation.TargetApi
import android.app.AlarmManager
import android.app.Dialog
import android.app.PendingIntent
import android.content.Intent
import android.content.IntentFilter
import android.content.res.Configuration
import android.graphics.RectF
Expand Down Expand Up @@ -161,33 +158,6 @@ open class TrimeInputMethodService : LifecycleInputMethodService() {
}
}

/** 防止重启系统 强行停止应用时alarm任务丢失 */
@SuppressLint("ScheduleExactAlarm")
fun restartSystemStartTimingSync() {
if (prefs.profile.timingBackgroundSyncEnabled) {
val triggerTime = prefs.profile.timingBackgroundSyncSetTime
val alarmManager = getSystemService(ALARM_SERVICE) as AlarmManager

/** 设置待发送的同步事件 */
val pendingIntent =
PendingIntent.getBroadcast(
this,
0,
Intent("com.osfans.trime.timing.sync"),
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE
} else {
PendingIntent.FLAG_UPDATE_CURRENT
},
)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { // 根据SDK设置alarm任务
alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, triggerTime, pendingIntent)
} else {
alarmManager.setExact(AlarmManager.RTC_WAKEUP, triggerTime, pendingIntent)
}
}
}

private fun registerReceiver() {
val intentFilter =
IntentFilter().apply {
Expand Down Expand Up @@ -231,7 +201,6 @@ open class TrimeInputMethodService : LifecycleInputMethodService() {
ColorManager.init(resources.configuration)
ThemeManager.init()
InputFeedbackManager.init()
restartSystemStartTimingSync()
val theme = ThemeManager.activeTheme
val defaultLocale = theme.generalStyle.locale.split(DELIMITER_SPLITTER)
locales[0] =
Expand Down

This file was deleted.

Loading
Loading